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();
        }
    }
}
相关推荐
运维-大白同学几秒前
go-数据库基本操作
开发语言·数据库·golang
动感光博11 分钟前
Unity(URP渲染管线)的后处理、动画制作、虚拟相机(Virtual Camera)
开发语言·人工智能·计算机视觉·unity·c#·游戏引擎
forestsea21 分钟前
Maven 插件扩展点与自定义生命周期
java·maven
蚰蜒螟1 小时前
深入解析JVM字节码解释器执行流程(OpenJDK 17源码实现)
开发语言·jvm·python
keke101 小时前
Java【14_2】接口(Comparable和Comparator)、内部类
java·开发语言
思茂信息1 小时前
CST软件对OPERA&CST软件联合仿真汽车无线充电站对人体的影响
c语言·开发语言·人工智能·matlab·汽车·软件构建
CN.LG1 小时前
Java 乘号来重复字符串的功能
java·开发语言
川川菜鸟1 小时前
2025长三角数学建模C题完整思路
c语言·开发语言·数学建模
萌新下岸多多关照1 小时前
Java中synchronized 关键字
java·开发语言
中国lanwp1 小时前
使用Maven部署WebLogic应用
java·maven