You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Java从FTP服务器下载选中的最新修改文件?

Java FTPClient 下载FTP最新修改文件实现方案

你使用的是Apache Commons Net组件的FTPClient,按以下修改即可完成下载功能:

完整可运行代码

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

public class FtpDownloadLatestFile {
    public static void main(String[] args) {
        String server = "ftp.swfwmd.state.fl.us";
        int port = 21;
        String username = "anonymous";
        String password = "ftp.swfwmd.state.fl.us";
        FTPClient client = new FTPClient();

        try {
            client.connect(server, port);
            boolean loginSuccess = client.login(username, password);
            if (!loginSuccess) {
                System.out.println("FTP登录失败,响应码:" + client.getReplyCode());
                return;
            }
            FTPFile[] files = client.listFiles();
            if (files == null || files.length == 0) {
                System.out.println("当前FTP目录无文件");
                return;
            }

            FTPFile lastFile = lastFileModified(files);
            // 下载逻辑
            if (lastFile.isFile()) {
                // 开启被动模式,解决防火墙拦截问题
                client.enterLocalPassiveMode();
                // 设置二进制传输模式,避免文件损坏
                client.setFileType(FTP.BINARY_FILE_TYPE);
                // 自定义本地保存路径
                String localPath = "./" + lastFile.getName();
                try (FileOutputStream fos = new FileOutputStream(localPath)) {
                    boolean downloadSuccess = client.retrieveFile(lastFile.getName(), fos);
                    if (downloadSuccess) {
                        System.out.println("下载成功,文件保存至:" + localPath);
                    } else {
                        System.out.println("下载失败,FTP响应码:" + client.getReplyCode());
                    }
                }
            } else {
                System.out.println("最新修改的条目为目录,无法下载:" + lastFile.getName());
            }
            // 登出FTP
            client.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 确保连接最终被关闭
            if (client.isConnected()) {
                try {
                    client.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static FTPFile lastFileModified(FTPFile[] files) {
        Date lastMod = files[0].getTimestamp().getTime();
        FTPFile choice = files[0];
        for (FTPFile file : files) {
            if (file.getTimestamp().getTime().after(lastMod)) {
                choice = file;
                lastMod = file.getTimestamp().getTime();
                System.out.println("最新修改时间: " + file.getTimestamp().getTime() + "\n文件名:  " + file.getName());
            }
        }
        return choice;
    }
}

关键配置说明

  • 被动模式开启:默认FTPClient使用主动模式,多数客户端网络的防火墙会拦截服务器的主动连接请求,调用enterLocalPassiveMode()可规避该问题
  • 传输模式设置:默认使用ASCII传输模式,下载二进制文件(图片、压缩包、exe等)会出现损坏,统一设置为FTP.BINARY_FILE_TYPE兼容性更好
  • 资源释放:新增finally块保证FTP连接无论是否发生异常都会被关闭,避免连接泄漏
  • 状态校验:新增登录、文件列表为空、文件类型校验逻辑,避免空指针等运行时异常

内容的提问来源于stack exchange,提问作者my pochta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 21:48:05