package net.alphawind.common.ftputil.service; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.springframework.stereotype.Service; import lombok.extern.slf4j.Slf4j; @Slf4j @Service public class FtpServiceImpl { /** * @param url FTP服务器IP * @param port FTP登录账号 * @param username FTP登录密码 * @param password 密码 * @param remotePath FTP服务器上的相对路径,支持yyyyMMddHHmmsss转换为对应的时间 * @param fileName 要下载的文件名,为空时默认下载所有 * @param localPath 下载后保存到本地的根路径 * @param contain 过滤条件,下载的文件名必需包含此字符串,为空时不做过滤 * @param unContain 过滤条件,下载的文件名不能包含此字符串,为空时不做过滤 * @return */ public List<File> downloadFtpFile( String host, int port, String username, String password, String remotePath, String fileName, String localPath, String contain, String unContain ) { List<File> downloadList = new ArrayList<>(); FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port); // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器 // ftp.connect(host); ftp.login(username, password);// 登录 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); } ftp.enterLocalPassiveMode(); // 被动模式 remotePath = replaceTimeStr(remotePath); ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录 FTPFile[] fs = ftp.listFiles(); List<FTPFile> downList = new ArrayList<>(); for (FTPFile ff : fs) { if(fileName.equals("")) { downList.add(ff); }else { if(ff.getName().equals(fileName)) { downList.add(ff); } } } if(!contain.equals("")) { List<FTPFile> downList1 = new ArrayList<>(); for (Iterator<FTPFile> iterator = downList.iterator(); iterator.hasNext();) { FTPFile ftpFile = iterator.next(); String name = ftpFile.getName(); if(name.contains(contain)) { downList1.add(ftpFile); } } downList = downList1; } if(!unContain.equals("")) { List<FTPFile> downList2 = new ArrayList<>(); for (Iterator<FTPFile> iterator = downList.iterator(); iterator.hasNext();) { FTPFile ftpFile = iterator.next(); String name = ftpFile.getName(); if(!name.contains(unContain)) { downList2.add(ftpFile); } } downList = downList2; } for (Iterator<FTPFile> iterator = downList.iterator(); iterator.hasNext();) { FTPFile ftpFile = iterator.next(); File f = new File(localPath + remotePath); if(!f.exists()) { f.mkdirs(); } File localFile = new File(localPath + remotePath + ftpFile.getName()); if(!localFile.exists()) { //当前文件是否已经存在 downloadList.add(localFile); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ftpFile.getName(), is); is.close(); } } ftp.logout(); } catch (IOException e) { log.error(e.getMessage()); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { log.error(ioe.getMessage()); } } } return downloadList; } private String replaceTimeStr(String remotePath) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String now = sdf.format(new Date()); String year = now.substring(0, 4); String month = now.substring(5, 7); String day = now.substring(8, 10); String hour = now.substring(11, 13); String minute = now.substring(14, 16); String second = now.substring(17, 19); remotePath = remotePath.replace("yyyy", year); remotePath = remotePath.replace("MM", month); remotePath = remotePath.replace("dd", day); remotePath = remotePath.replace("HH", hour); remotePath = remotePath.replace("mm", minute); remotePath = remotePath.replace("ss", second); return remotePath; } }
ftp传输
最新推荐文章于 2024-10-01 17:54:24 发布