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; } }