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();
        }
    }
}
相关推荐
Grey Zeng21 小时前
Java SE 25新增特性
java·jdk·jdk新特性·jdk25
雨白1 天前
Java 线程通信基础:interrupt、wait 和 notifyAll 详解
android·java
架构师沉默1 天前
设计多租户 SaaS 系统,如何做到数据隔离 & 资源配额?
java·后端·架构
Java中文社群1 天前
重要:Java25正式发布(长期支持版)!
java·后端·面试
每天进步一点_JL1 天前
JVM 类加载:双亲委派机制
java·后端
用户298698530141 天前
Java HTML 转 Word 完整指南
java·后端
渣哥1 天前
原来公平锁和非公平锁差别这么大
java
渣哥1 天前
99% 的人没搞懂:Semaphore 到底是干啥的?
java
J2K1 天前
JDK都25了,你还没用过ZGC?那真得补补课了
java·jvm·后端
kfyty7251 天前
不依赖第三方,不销毁重建,loveqq 框架如何原生实现动态线程池?
java·架构