• 使用FTPClient实现文件上传服务器


    import ch.qos.logback.classic.Logger;
    import org.apache.commons.net.ftp.*;
    import org.slf4j.LoggerFactory;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.List;
    
    public class uploadutil {
        public final static Logger logger= (Logger) LoggerFactory.getLogger(uploadutil.class);
        private static String ftpip=propertiesutil.getValue("ftp.server.ip");
        private static String ftpuser=propertiesutil.getValue("ftp.user");
        private static String ftppass=propertiesutil.getValue("ftp.password");
    
        private int port;
        private String ip;
        private String user;
        private String pass;
        private FTPClient ftpClient;
    
        public uploadutil(int port, String ip, String user, String pass) {
            this.port = port;
            this.ip = ip;
            this.user = user;
            this.pass = pass;
        }
    
        public String getUser() {
            return user;
        }
    
        public void setUser(String user) {
            this.user = user;
        }
    
        public int getPort() {
            return port;
        }
    
        public void setPort(int port) {
            this.port = port;
        }
    
        public String getIp() {
            return ip;
        }
    
        public void setIp(String ip) {
            this.ip = ip;
        }
    
        public FTPClient getFtpClient() {
            return ftpClient;
        }
    
        public void setFtpClient(FTPClient ftpClient) {
            this.ftpClient = ftpClient;
        }
    
        public String getPass() {
            return pass;
        }
    
        public void setPass(String pass) {
            this.pass = pass;
        }
    public  static boolean uploadok(List<File> filelist) throws IOException {
        uploadutil u=new uploadutil(14147,ftpip,ftpuser,ftppass);
        logger.info("start upload");
        boolean result=u.uploads("work",filelist);
        logger.info("end upload");
        return result;
    }
    private boolean uploads(String remotepath,List<File> listfile) throws IOException {
        boolean upload=true;
        FileInputStream f=null;
        if(connectserver(this.getPort(),this.getIp(),this.getUser(),this.pass)){
            try {
                ftpClient.changeWorkingDirectory(remotepath);
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("utf-8");
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                for(File file:listfile){
                    f=new FileInputStream(file);
                    ftpClient.storeFile(file.getName(),f);
                }
            } catch (IOException e) {
                logger.error("文件上传异常",e);
                upload=false;
                e.printStackTrace();
            }finally {
                f.close();
                ftpClient.disconnect();
            }
    
        }
        return upload;
    }
        private boolean connectserver(int port, String ip, String user, String pass){
            ftpClient=new FTPClient();
            boolean isok=false;
            try {
                ftpClient.connect(ip);
                isok=ftpClient.login(user,pass);
            } catch (IOException e) {
                logger.error("链接服务器异常",e);
            }
            return isok;
        }
    
    
    }
    

      

  • 相关阅读:
    dreamvc框架(一)ioc容器的集成
    1040. Longest Symmetric String (25)
    虚指针存在证明及虚函数表
    Kibana中doc与search策略的区别
    AngularJS API之bootstrap启动
    AngularJS 技术总结
    Elasticsearch Javascript API增删改查
    AngularJS 中的Promise --- $q服务详解
    Ruby编程实践
    Ruby测试小代码[计算50以内的素数]
  • 原文地址:https://www.cnblogs.com/qinyios/p/11063497.html
Copyright © 2020-2023  润新知