java FTP获取文件夹中所有文件

1、FtpUtils工具类

package pactera.tf.dl.common.util;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.IOException;

public class FtpUtils {

    /**
     * ftp链接
     * @return
     * @throws IOException
     */
    public static FTPClient ftpConnection(String ip,int port,String username,String password) throws IOException {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(ip, port);
            ftpClient.login(username, password);
            int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器
            if(!FTPReply.isPositiveCompletion(replyCode)) {
                ftpClient.disconnect();
                System.out.println("--ftp连接失败--");
                System.exit(1);
            }else {
                System.out.println("--ftp连接成功--");
            }
            ftpClient.enterLocalPassiveMode();//这句最好加告诉对面服务器开一个端口
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ftpClient;
    }

    /**
     * 断开FTP连接
     * @param ftpClient  初始化的对象
     * @throws IOException
     */
    public static void close(FTPClient ftpClient) throws IOException{
        if(ftpClient!=null && ftpClient.isConnected()){
            ftpClient.logout();
            ftpClient.disconnect();
        }
    }

}

2、解析文件数据

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;

public void coreDataWarehouse() throws Exception{
        //获取昨天的时间
        String yesterday = DateUtils.getChangeDate(-1);
        //ftp地址
        String ftpip = dlSysCfgManager.getVal(CfgSystemEnum.FTP_HOST.getCode());
        //ftp端口
        int ftpport = Integer.parseInt(dlSysCfgManager.getVal(CfgSystemEnum.FTP_PORT.getCode()));
        //ftp用户名
        String ftpusername = dlSysCfgManager.getVal(CfgSystemEnum.FTP_USERNAME.getCode());
        //ftp密码
        String ftppassword = dlSysCfgManager.getVal(CfgSystemEnum.FTP_PASSWORD.getCode());
        //连接ftp
        FTPClient ftpClient = FtpUtils.ftpConnection(ftpip,ftpport,ftpusername,ftppassword);
        //ftp文件夹
        String fileFolder = dlSysCfgManager.getVal(CfgSystemEnum.FTP_FILEFOLDER.getCode());
        String fileName = fileFolder + "/" + yesterday;

        try{
            ftpClient.changeWorkingDirectory(fileName);
            FTPFile[] files = ftpClient.listFiles();
            if(files != null && files.length > 0){
                for (FTPFile file : files){
                    if(file.getName().endsWith("rdy") || !(file.getName().contains(SysPrefixEnum.CORE.getCode()))){
                        continue;
                    }

                    //获取核心表数据
                    InputStream fileInputStream = ftpClient.retrieveFileStream(file.getName());
                    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
                    BufferedReader bufferRead = new BufferedReader(inputStreamReader);
                    String readLine = "";
                    while (!StringUtils.isEmpty(readLine = bufferRead.readLine())) {
                        System.out.println(readLine );
                    }

                    bufferRead.close();
                    inputStreamReader.close();
                    fileInputStream.close();
                }
            }
        }catch (Exception e){
            throw new DlCommonException(e, DlCommonErrorCodeEnum.EERROR_1007);
        }finally {
            FtpUtils.close(ftpClient);
        }
    }
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值