import org.apache.commons.lang.StringUtils; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClientConfig; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import java.io.*; import java.util.function.Function;
@Component public class FtpHelper implements Closeable { private final static Logger log = LoggerFactory.getLogger(FtpHelper.class); private FTPClient ftp = null; boolean _isLogin = false; public static FtpHelper getInstance() { return new FtpHelper(); }
public boolean login(String ip,int port){ return login(ip, port,"anonymous",""); }
public boolean login(String ip,int port, String uname, String pass) { ftp = new FTPClient();
try { ftp.connect(ip,port); _isLogin = ftp.login(uname, pass); log.info(_isLogin?"登录成功":"登录失败"); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { System.err.println("FTP服务器拒绝连接 "); return false; } return true; } catch (Exception ex) { ex.printStackTrace(); return false; } }
public Function<FtpFileInfo, Boolean> onUploadFileAfter;
public boolean uploadFile(String localFileName ,String ftpDirName , String ftpFileName) { return uploadFile(localFileName, ftpDirName, ftpFileName,false); }
public boolean uploadFile(String localFileName , String ftpDirName , String ftpFileName , boolean deleteLocalFile) { if(StringUtils.isEmpty(ftpFileName)) throw new RuntimeException("上传文件必须填写文件名!"); File srcFile = new File(localFileName); if(!srcFile.exists()) throw new RuntimeException("文件不存在:"+localFileName); try (FileInputStream fis = new FileInputStream(srcFile)) { boolean flag = uploadFile(fis,ftpDirName,ftpFileName); if(onUploadFileAfter!=null){ onUploadFileAfter.apply(new FtpFileInfo(localFileName,ftpDirName,ftpFileName)); } if(deleteLocalFile){ srcFile.delete(); log.info("ftp删除源文件:{0}",srcFile); } fis.close(); return flag; } catch (Exception e) { e.printStackTrace(); return false; } finally { } }
public boolean uploadFile(FileInputStream uploadInputStream ,String ftpDirName , String ftpFileName) { if(StringUtils.isEmpty(ftpFileName)) throw new RuntimeException("上传文件必须填写文件名!"); try { if(!createDir(ftpDirName)){ throw new RuntimeException("切入FTP目录失败:"+ftpDirName); } ftp.setBufferSize(1024); ftp.setControlEncoding("GBK"); FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT); conf.setServerLanguageCode("zh"); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); String fileName = new String(ftpFileName.getBytes("GBK"),"iso-8859-1"); if(ftp.storeFile(fileName, uploadInputStream)){ uploadInputStream.close(); log.info("文件上传成功"); return true; } return false; } catch (Exception e) { e.printStackTrace(); return false; } finally { } }
public boolean downloadFile(String ftpDirName, String ftpFileName, String localFileFullName) { try { if("".equals(ftpDirName)) ftpDirName="/"; String dir = new String(ftpDirName.getBytes("GBK"),"iso-8859-1"); if(!ftp.changeWorkingDirectory(dir)){ log.info("切换目录失败:"+ftpDirName); return false; } FTPFile[] fs = ftp.listFiles(); String fileName = new String(ftpFileName.getBytes("GBK"),"iso-8859-1"); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { FileOutputStream is = new FileOutputStream(new File(localFileFullName)); ftp.retrieveFile(ff.getName(), is); is.close(); log.info("下载ftp文件已下载:"+localFileFullName); return true; } } log.info("下载ftp文件失败:"+ftpFileName+";目录:"+ftpDirName); return false; } catch (Exception e) { e.printStackTrace(); return false; } }
public boolean removeFile(String ftpFileName) { boolean flag ; try { ftpFileName = new String(ftpFileName.getBytes("GBK"),"iso-8859-1"); flag = ftp.deleteFile(ftpFileName); log.info(flag?"成功":"失败"); return flag; } catch (IOException e) { e.printStackTrace(); return false; } }
public boolean removeDir(String dir){ if(StringUtil.startWith(dir, "/")) dir="/"+dir; try { String d = new String(dir.toString().getBytes("GBK"),"iso-8859-1"); return ftp.removeDirectory(d); } catch (Exception e) { e.printStackTrace(); return false; } }
public boolean createDir(String dir){ if(StringUtils.isEmpty(dir)) return true; String d; try { d = new String(dir.toString().getBytes("GBK"),"iso-8859-1"); if(ftp.changeWorkingDirectory(d)) return true; dir = StringUtil.trimStart(dir, "/"); dir = StringUtil.trimEnd(dir, "/"); String[] arr = dir.split("/"); StringBuffer sbfDir=new StringBuffer(); for(String s : arr){ sbfDir.append("/"); sbfDir.append(s); d = new String(sbfDir.toString().getBytes("GBK"),"iso-8859-1"); if(ftp.changeWorkingDirectory(d)) continue; if(!ftp.makeDirectory(d)){ log.info("[失败]ftp创建目录:"+sbfDir.toString()); return false; } log.info("[成功]创建ftp目录:"+sbfDir.toString()); } return ftp.changeWorkingDirectory(d); } catch (Exception e) { e.printStackTrace(); return false; } }
private void closeFtpConnection() { _isLogin = false; if (ftp != null) { if (ftp.isConnected()) { try { ftp.logout(); ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } }
@Override public void close() { this.closeFtpConnection(); } public static class FtpFileInfo{ public FtpFileInfo(String srcFile,String ftpDirName,String ftpFileName){ this.ftpDirName=ftpDirName; this.ftpFileName=ftpFileName; this.srcFile=srcFile; } String srcFile; String ftpDirName; String ftpFileName; String ftpFileFullName; public String getSrcFile() { return srcFile; } public void setSrcFile(String srcFile) { this.srcFile = srcFile; } public String getFtpDirName() { return ftpDirName; } public void setFtpDirName(String ftpDirName) { this.ftpDirName = ftpDirName; } public String getFtpFileName() { return ftpFileName; } public void setFtpFileName(String ftpFileName) { this.ftpFileName = ftpFileName; }
public String getFtpFileFullName() { return StringUtil.Combine("/",ftpDirName,ftpFileName); } } }
|