ftp传输

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值