杰瑞科技汇

Java中如何判断FTP文件是否存在?

Java 检查 FTP 文件是否存在

在 Java 中,你可以使用 Apache Commons Net 库来检查 FTP 服务器上文件是否存在,以下是几种实现方法:

使用 FTPClient 的 listFiles()

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public boolean checkFileExists(String server, int port, String user, String password, String remotePath) {
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(server, port);
        ftpClient.login(user, password);
        ftpClient.enterLocalPassiveMode(); // 重要:被动模式
        FTPFile[] files = ftpClient.listFiles(remotePath);
        return files != null && files.length > 0;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用 FTPClient 的 retrieveFile()

public boolean checkFileExists(String server, int port, String user, String password, String remotePath) {
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(server, port);
        ftpClient.login(user, password);
        ftpClient.enterLocalPassiveMode();
        // 尝试获取文件输入流,如果文件不存在会抛出异常
        InputStream inputStream = ftpClient.retrieveFileStream(remotePath);
        boolean success = inputStream != null;
        if (inputStream != null) {
            inputStream.close();
        }
        ftpClient.completePendingCommand(); // 必须调用以完成命令
        return success;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用 FTPClient 的 size()

public boolean checkFileExists(String server, int port, String user, String password, String remotePath) {
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(server, port);
        ftpClient.login(user, password);
        ftpClient.enterLocalPassiveMode();
        long fileSize = ftpClient.getSize(remotePath);
        return fileSize > -1; // 返回-1表示文件不存在
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注意事项

  1. 被动模式enterLocalPassiveMode() 对于大多数防火墙和NAT环境很重要
  2. 异常处理:需要妥善处理各种IO异常和FTP异常
  3. 连接管理:确保在finally块中关闭连接
  4. 路径格式:确保远程路径格式正确(通常以/开头)
  5. 依赖:需要添加Apache Commons Net库依赖

Maven 依赖

<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.9.0</version> <!-- 使用最新版本 -->
</dependency>

方法都可以用来检查FTP服务器上文件是否存在,选择哪种方法取决于你的具体需求和FTP服务器的配置。

分享:
扫描分享到社交APP
上一篇
下一篇