ftp工具类(详解)

今日份工作 ,从远程ftp文件服务器获取文件。参考了网络上的ftp工具报错,干脆自己写一个得了。废话不多说,直接撸代码

写好
1.首先建立自己的boot项目 moudle
在这里插入图片描述
这里我是用了自动任务在跑,当然你也可以自己改成main方法。
1.编写自己的perproties文件

在这里插入图片描述
2.编写连接ftp服务器得工具

package ftp.util;


import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

/**
 * @author lihonglin-jsb
 * @Date 2021/10/23 15:04
 */

@Component
public class FtpConnect {
    private static Logger logger = LoggerFactory.getLogger(FtpConnect.class);

    // FTP 登录用户名
    @Value("${ftp.client.username}")
    private String userName;
    // FTP 登录密码
    @Value("${ftp.client.password}")
    private String pwd;
    // FTP 服务器地址IP地址
    @Value("${ftp.client.host}")
    private String host;
    // FTP 端口
    @Value("${ftp.client.port}")
    private int port;

    /**
     * 连接ftp
     *
     * @return
     * @throws Exception
     */
    public FTPClient getFTPClient() {
        FTPClient ftpClient = new FTPClient();
        //设置ftp字符集
        ftpClient.setControlEncoding("utf-8");

        try {
            ftpClient = new FTPClient();
            logger.info("地址:" + host + ":" + port);
            // 连接FTP服务器
            ftpClient.connect(host);
            logger.info("用户名:" + userName);
            // 登陆FTP服务器
            ftpClient.login(userName, pwd);

            //ftp主动模式
            ftpClient.enterLocalActiveMode();

            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                int replyCode = ftpClient.getReplyCode();
                System.out.println(replyCode);
                logger.info("未连接到FTP,用户名或密码错误。");
                ftpClient.disconnect();
            } else {
                logger.info("FTP连接成功。");
            }
            // 设置被动模式
            ftpClient.enterLocalPassiveMode();
            //设置二进制传输
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        } catch (SocketException e) {
            logger.error("连接ftp失败!");
            logger.info("FTP的IP地址可能错误,请正确配置。");
        } catch (IOException e) {
            logger.error("连接ftp失败!");
            logger.error(e.toString());
            logger.info("FTP的端口错误,请正确配置。");
        }
        return ftpClient;
    }

    /**
     * 关闭连接
     *
     * @param ftpClient
     */
    public void close(FTPClient ftpClient) {
        try {
            if (ftpClient != null) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException e) {
            logger.error("ftp连接关闭失败!");
        }
    }
}

3.接着编写java文件下载得工具类

package ftp.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class FtpUtil {

    private static Logger logger = LoggerFactory.getLogger(FtpUtil.class);
    public static final String DIRSPLIT = "/";

    public static File downloadFile(FTPClient ftpClient, String targetPath, String filetype) throws Exception {

        OutputStream outputStream = null;
        try {
            File directory = new File(".");
            String path = null;
            path = "F:\\test";// 获取
            logger.info("当前路径" + path);
            path = path + DIRSPLIT + filetype;
            logger.info("目录路径" + path);
            File fileDire = new File(path);
            if (!fileDire.exists() && !fileDire.isDirectory()) {
                fileDire.mkdirs();
            }
            path = path + DIRSPLIT + targetPath.substring(targetPath.lastIndexOf("/") + 1);
            logger.info("文件路径" + path);
            File file = new File(path);
            if (!file.exists()) {
                if (!file.createNewFile()) {
                    logger.info("创建文件失败!");
                    return null;
                }

            }
            outputStream = new FileOutputStream(file);
            ftpClient.retrieveFile(targetPath, outputStream);
            logger.info("Download file success. TargetPath: {}", targetPath);
            return file;
        } catch (Exception e) {
            logger.error("Download file failure. TargetPath: {}", targetPath);
            throw new Exception("Download File failure");
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }

    /**
     * 删除文件
     *
     * @param
     * @return
     * @throws IOException
     */
    public static boolean deleteFile(File file) {
        boolean result = false;
        if (file.exists()) {
            if (file.delete()) {
                result = true;
            }
        }
        return result;
    }

    /**
     * 处理文件名绝对路径
     *
     * @param filePath
     * @param fileName
     * @return P:/temp/1.txt 或者 p:/temp/x
     */
    public static String pasreFilePath(String filePath, String fileName) {
        StringBuffer sb = new StringBuffer();
        if (filePath.endsWith("/") || filePath.endsWith("\\")) {
            sb.append(filePath).append(fileName);
        } else {
            sb.append(filePath.replaceAll("\\\\", "/")).append("/").append(fileName);
        }
        return sb.toString();
    }

    /**
     * 获取今天日期 - 数字格式
     *
     * @return yyyyMMdd
     */
    public static String getCurrentday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 0);
        return new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
    }

    /**
     * 获取昨天日期 - 数字格式
     *
     * @return yyyyMMdd
     */
    public static String getYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        return new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
    }
}

4.编写自动任务

package ftp.Task;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ftp.util.FtpConnect;
import ftp.util.FtpUtil;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author lihonglin-jsb
 * @Date 2021/10/23 15:06
 */

@Component
public class TaskSchedule {
    private static Logger logger = LoggerFactory.getLogger(TaskSchedule.class);

    @Autowired
    private FtpConnect connect;

    private long sleepTime = 60000;
    private long total = 10;
    private long num = 0;
    public static final String UNDERLINE = "_";
    public static final String preFileName = "10020048860000_";
    public static final String suffixType=".xls";


    @Scheduled(cron = "0/5 * * * * ?")
    public String importSPserviceinfo() {
        String readSPservicePathDay = "/build/";
        logger.info("文件路径:" + readSPservicePathDay);

        // 获取远程目录下的文件到容器
        List<File> files = sftpGet(".xls", readSPservicePathDay);
        System.out.println("我进来啦");
        return "0000";
    }


    /**
     * 获取远程目录下的文件到容器
     */
    public List<File> sftpGet(String filetype, String path) {
        FTPClient ftpClient = null;
        // 获取昨天日期
        String yesterday = FtpUtil.getYesterday();
        FTPFile[] ftpFiles = null;
        List<File> files = new ArrayList<File>();
        String filetypeDate = preFileName  ;


        try {
            ftpClient = connect.getFTPClient();
            // 跳转到指定目录
            ftpClient.changeWorkingDirectory(path);
        } catch (Exception e1) {
            logger.error("ftp连接异常");
        }

        try {
            //ftp client告诉ftp server开通一个端口来传输数据
            ftpClient.enterLocalPassiveMode();
            logger.info("获得指定目录下的文件夹和文件信息");
            ftpFiles = ftpClient.listFiles();

            for (int i = 0; i < ftpFiles.length; i++) {
                FTPFile ftpfile = ftpFiles[i];
                String name = ftpfile.getName();
                if (".".equals(name) || "..".equals(name) || ftpfile.isDirectory()) {
                    continue;
                }

                if (name.startsWith(filetypeDate) && name.endsWith(suffixType)) {
                    logger.info("获取到目录下文件名:" + name);
                    // 远程服务器
                    String sftpRemoteAbsolutePath = FtpUtil.pasreFilePath(path, name);
                    File file = FtpUtil.downloadFile(ftpClient, sftpRemoteAbsolutePath, filetype);
                    files.add(file);
                }
            }
            if (files.isEmpty()) {
                throw new Exception();
            }

        } catch (Exception e) {
            logger.error(" sftpGet  error");
            logger.error(e.toString());
            logger.error("次数" + num);
            if (num == total) {
                num = 0;
                throw new RuntimeException(e);
            }
            try {
                num += 1;
                Thread.sleep(sleepTime);
                importSPserviceinfo();
            } catch (InterruptedException e1) {
                logger.error("获取文件失败");
                Thread.currentThread().interrupt();
            }
        } finally {
            connect.close(ftpClient);
        }
        return files;
    }
}

总结:用ftp工具连接远程的时候注意ip+端口那边的变量,请自行替换

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值