Java FTP按关键字批量下载文件

一、所需jar

java 复制代码
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.8.0</version>
        </dependency>

二、工具类

java 复制代码
import java.io.*;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FTPBulkFileDownloader {

    public static void downloadFiles(String server, int port, String user, String pass, String remoteDir, String saveDir, String keyword) throws IOException {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            List<String> filesToDownload = getFilesToDownload(ftpClient, remoteDir, keyword);
            for (String file : filesToDownload) {
                String remoteFilePath = remoteDir + "/" + file;
                String saveFilePath = saveDir + File.separator + file;
                downloadFile(ftpClient, remoteFilePath, saveFilePath);
            }
        } finally {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        }
    }

    private static List<String> getFilesToDownload(FTPClient ftpClient, String remoteDir, String keyword) throws IOException {
        List<String> filesToDownload = new ArrayList<>();
        FTPFile[] files = ftpClient.listFiles(remoteDir);
        for (FTPFile file : files) {
            if (file.isFile() && file.getName().contains(keyword)) {
                filesToDownload.add(file.getName());
            }
        }
        return filesToDownload;
    }

    private static void downloadFile(FTPClient ftpClient, String remoteFilePath, String saveFilePath) throws IOException {
        InputStream inputStream = ftpClient.retrieveFileStream(remoteFilePath);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(saveFilePath);
            int bytesRead;
            byte[] buffer = new byte[1024];
            while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }
            System.out.println("File downloaded: " + saveFilePath);
        } finally {
            if (bufferedInputStream != null) {
                bufferedInputStream.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
            ftpClient.completePendingCommand();
        }
    }
    public static void main(String[] args) {
        String server = "FTPIP";
        //FTP端口
        int port = 21;
        //用户名
        String user = "";
        //密码
        String pass = "";
        //FTP文件目录
        String remoteDir = "/FTP";
        //本地文件存放目录
        String saveDir = "/local";
        //关键字
        String keyword = "222";

        try {
            downloadFiles(server, port, user, pass, remoteDir, saveDir, keyword);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
相关推荐
我命由我123452 分钟前
Android 开发中,关于 Gradle 的 distributionUrl 的一些问题
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
橙露3 分钟前
SpringBoot 全局异常处理:优雅封装统一返回格式
java·spring boot·后端
awei091614 分钟前
MinIO配置自定义crossdomain.xml跨域策略(Nginx反向代理实现)
xml·java·nginx
谁怕平生太急24 分钟前
面试题记录:在线数据迁移
java·数据库·spring
朝阳58125 分钟前
rust 交叉编译指南
开发语言·后端·rust
木井巳30 分钟前
【递归算法】组合总和
java·算法·leetcode·决策树·深度优先·剪枝
量子炒饭大师1 小时前
【C++ 进阶】Cyber霓虹掩体下的代码拟态——【面向对象编程 之 多态】一文带你搞懂C++面向对象编程中的三要素之一————多态!
开发语言·c++·多态
消失的旧时光-19431 小时前
Spring Boot 入门实战(二):用户注册接口设计(Controller + DTO + Validation)
java·spring boot·接口
xiaoshuaishuai82 小时前
C# 实现百度搜索算法逆向
开发语言·windows·c#·dubbo
A-Jie-Y2 小时前
JAVA框架-SpringBoot环境搭建指南
java·spring boot