杰瑞科技汇

Java FTP目录如何判断是否存在?

Java FTP 检查目录是否存在

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

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

使用 listFiles() 方法

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public boolean isDirectoryExists(FTPClient ftpClient, String dirPath) throws IOException {
    FTPFile[] files = ftpClient.listFiles(dirPath);
    return files != null && files.length > 0;
}

使用 changeWorkingDirectory() 方法

import org.apache.commons.net.ftp.FTPClient;
public boolean isDirectoryExists(FTPClient ftpClient, String dirPath) throws IOException {
    // 保存当前目录
    String originalDirectory = ftpClient.printWorkingDirectory();
    try {
        // 尝试切换到目标目录
        return ftpClient.changeWorkingDirectory(dirPath);
    } finally {
        // 恢复原始目录
        ftpClient.changeWorkingDirectory(originalDirectory);
    }
}

使用 listNames() 方法(更高效)

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public boolean isDirectoryExists(FTPClient ftpClient, String dirPath) throws IOException {
    // 确保使用正确的文件列表格式
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    // 获取目录列表
    String[] names = ftpClient.listNames(dirPath);
    // 如果返回非null数组,则目录存在
    return names != null && names.length > 0;
}

完整示例

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 FTPDirectoryChecker {
    public static void main(String[] args) {
        String server = "ftp.example.com";
        int port = 21;
        String user = "username";
        String pass = "password";
        String dirPath = "/path/to/directory";
        FTPClient ftpClient = new FTPClient();
        try {
            // 连接FTP服务器
            ftpClient.connect(server, port);
            int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                System.out.println("FTP服务器连接失败");
                return;
            }
            // 登录
            if (!ftpClient.login(user, pass)) {
                System.out.println("FTP登录失败");
                return;
            }
            // 检查目录是否存在
            if (isDirectoryExists(ftpClient, dirPath)) {
                System.out.println("目录存在: " + dirPath);
            } else {
                System.out.println("目录不存在: " + dirPath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static boolean isDirectoryExists(FTPClient ftpClient, String dirPath) throws IOException {
        // 方法1: 使用listFiles()
        /*
        FTPFile[] files = ftpClient.listFiles(dirPath);
        return files != null && files.length > 0;
        */
        // 方法2: 使用changeWorkingDirectory()
        /*
        String originalDirectory = ftpClient.printWorkingDirectory();
        try {
            return ftpClient.changeWorkingDirectory(dirPath);
        } finally {
            ftpClient.changeWorkingDirectory(originalDirectory);
        }
        */
        // 方法3: 使用listNames() (推荐)
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        String[] names = ftpClient.listNames(dirPath);
        return names != null && names.length > 0;
    }
}

注意事项

  1. 确保添加了 Apache Commons Net 依赖:

    <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>3.9.0</version>
    </dependency>
  2. 某些FTP服务器可能需要设置被动模式:

    ftpClient.enterLocalPassiveMode();
  3. 目录路径应该使用正斜杠(),即使是在Windows系统上。

  4. 方法3(使用listNames())通常是最高效的,因为它只获取名称而不获取完整文件信息。

    Java FTP目录如何判断是否存在?-图2
    (图片来源网络,侵删)
  5. 如果目录为空,listFiles()listNames()方法仍然会返回结果,因此可以可靠地判断目录是否存在。

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