杰瑞科技汇

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

在Java中判断FTP服务器上文件是否存在

在Java中判断FTP服务器上文件是否存在,有几种常用的方法,以下是几种实现方式:

Java FTP如何判断文件是否存在?-图1
(图片来源网络,侵删)

方法1:使用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 isFileExists(FTPClient ftpClient, String remoteFilePath) throws IOException {
    FTPFile[] files = ftpClient.listFiles(remoteFilePath);
    return files != null && files.length > 0;
}

方法2:使用FTPClient的listNames方法

import org.apache.commons.net.ftp.FTPClient;
public boolean isFileExists(FTPClient ftpClient, String remoteFilePath) throws IOException {
    String[] names = ftpClient.listNames(remoteFilePath);
    return names != null && names.length > 0;
}

方法3:使用FTPClient的retrieveFile方法(不推荐,效率低)

import org.apache.commons.net.ftp.FTPClient;
public boolean isFileExists(FTPClient ftpClient, String remoteFilePath) throws IOException {
    InputStream inputStream = null;
    try {
        inputStream = ftpClient.retrieveFileStream(remoteFilePath);
        return inputStream != null;
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        ftpClient.completePendingCommand();
    }
}

方法4:使用FTPClient的size方法(如果FTP服务器支持)

import org.apache.commons.net.ftp.FTPClient;
public boolean isFileExists(FTPClient ftpClient, String remoteFilePath) throws IOException {
    long size = ftpClient.getSize(remoteFilePath);
    return size != -1;
}

完整示例

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.IOException;
public class FTPFileChecker {
    public static void main(String[] args) {
        String server = "ftp.example.com";
        int port = 21;
        String user = "username";
        String pass = "password";
        String remoteFilePath = "/path/to/your/file.txt";
        FTPClient ftpClient = new FTPClient();
        try {
            // 连接FTP服务器
            ftpClient.connect(server, port);
            int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                throw new IOException("FTP server refused connection.");
            }
            // 登录
            if (!ftpClient.login(user, pass)) {
                throw new IOException("FTP login failed.");
            }
            // 设置文件类型为二进制
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            // 检查文件是否存在
            boolean fileExists = isFileExists(ftpClient, remoteFilePath);
            System.out.println("文件是否存在: " + fileExists);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static boolean isFileExists(FTPClient ftpClient, String remoteFilePath) throws IOException {
        // 方法1: 使用listFiles
        FTPFile[] files = ftpClient.listFiles(remoteFilePath);
        if (files != null && files.length > 0) {
            return true;
        }
        // 方法2: 使用listNames
        String[] names = ftpClient.listNames(remoteFilePath);
        if (names != null && names.length > 0) {
            return true;
        }
        // 方法3: 使用getSize(如果FTP服务器支持)
        long size = ftpClient.getSize(remoteFilePath);
        if (size != -1) {
            return true;
        }
        return false;
    }
}

注意事项

  1. 需要添加Apache Commons Net库依赖:

    <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>3.9.0</version>
    </dependency>
  2. 不同的FTP服务器可能对某些命令的支持不同,方法3和方法4可能不适用于所有FTP服务器。

  3. 在多线程环境中使用FTPClient时,需要确保每个线程有自己的FTPClient实例。

  4. 处理完FTP操作后,记得关闭连接以释放资源。

    Java FTP如何判断文件是否存在?-图2
    (图片来源网络,侵删)
  5. 对于大型文件系统,listFiles方法可能会比较慢,可以考虑使用listNames方法作为替代。

Java FTP如何判断文件是否存在?-图3
(图片来源网络,侵删)
分享:
扫描分享到社交APP
上一篇
下一篇