ftp 文件读取,文件移动

FTPClient ftp = null;
List<EtcOrderVo> etcOrderVos = new ArrayList<>();
try {
    ftp = getFtpClient();
    boolean changeWorkingDirectoryFlag = ftp.changeWorkingDirectory("/");
    logger.info("转换路径是否成功={}", changeWorkingDirectoryFlag);
    // 获得指定目录下所有文件名
    FTPFile[] ftpFiles = ftp.listFiles("/");
    for (int i = 0; ftpFiles != null && i < ftpFiles.length; i++) {
        operateFile(ftp, ftpFiles[i], etcOrderVos);
    }
    ftp.logout();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (ftp.isConnected()) {
        try {
            ftp.disconnect();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

 

/**
 * 处理文件数据
 *
 * @param ftp
 * @param ftpFile
 * @param etcOrderVos
 * @throws IOException
 */
private List<EtcOrderVo> operateFile(FTPClient ftp, FTPFile ftpFile, List<EtcOrderVo> etcOrderVos) throws IOException {
    FTPFile file = ftpFile;
    if (!file.isFile()) {
        return etcOrderVos;
    }
    InputStream ins = null;
    BufferedReader reader = null;
    try {
        // 从服务器上读取指定的文件
        ins = ftp.retrieveFileStream(file.getName());
        reader = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
        String line;
        int a = 0;
        while ((line = reader.readLine()) != null) {
            logger.info("line=" + line);
            if (a >= 1) {
                String[] str = line.split("\\|!");
                if ("1".equals(str[0])) {
                    String etcApplyNo = str[1];
                    String status = str[3];
                    EtcOrderVo etcOrderVo = new EtcOrderVo();
                    etcOrderVo.setEtcApplyNo(etcApplyNo);
                    etcOrderVo.setStatus(Integer.valueOf(status) == 0 ? 2 : 3);//2通过,3不通过
                    etcOrderVos.add(etcOrderVo);
                    logger.info("etc单号={}", etcApplyNo);
                    logger.info("etc该单号的状态={}", status);
                }
            }
            a++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (ins != null) {
            ins.close();
            ftp.completePendingCommand();
        }
    }
    logger.info("是否转移成功" + ftp.rename(file.getName(), "/done/" + file.getName()));
    return etcOrderVos;
}

/**
 * 获取ftp连接
 * @return
 * @throws IOException
 */
private FTPClient getFtpClient() throws IOException {
    FTPClient ftp = new FTPClient();
    ftp.connect(ftpId);
    ftp.login(ftpUserName, ftpPassword);
    int reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
    }
    ftp.enterLocalPassiveMode();
    //设置传输方式为流方式
    ftp.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
    return ftp;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值