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();
        }
    }
}
相关推荐
碳水加碳水8 分钟前
Java代码审计实战:XML外部实体注入(XXE)深度解析
java·安全·web安全·代码审计
努力也学不会java1 小时前
【设计模式】 原型模式
java·设计模式·原型模式
方渐鸿2 小时前
【2024】k8s集群 图文详细 部署安装使用(两万字)
java·运维·容器·kubernetes·k8s·运维开发·持续部署
学亮编程手记2 小时前
K8S v1.33 版本主要新特性介绍
java·容器·kubernetes
Haven-3 小时前
Java-面试八股文-JVM篇
java·jvm·面试
我真的是大笨蛋3 小时前
JVM调优总结
java·jvm·数据库·redis·缓存·性能优化·系统架构
wjs0403 小时前
Git常用的命令
java·git·gitlab
superlls3 小时前
(算法 哈希表)【LeetCode 349】两个数组的交集 思路笔记自留
java·数据结构·算法
honder试试4 小时前
焊接自动化测试平台图像处理分析-模型训练推理
开发语言·python
^Rocky4 小时前
JavaScript性能优化实战
开发语言·javascript·性能优化