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();
        }
    }
}
相关推荐
程序员张33 分钟前
Maven编译和打包插件
java·spring boot·maven
ybq195133454311 小时前
Redis-主从复制-分布式系统
java·数据库·redis
weixin_472339462 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
小毛驴8502 小时前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
枯萎穿心攻击2 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
DKPT3 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
Eiceblue4 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
好奇的菜鸟4 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
m0_555762904 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
DuelCode5 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis