在Java中判断FTP目录是否存在
在Java中,你可以使用Apache Commons Net库中的FTPClient类来判断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;
}
方法2:使用changeWorkingDirectory()方法
import org.apache.commons.net.ftp.FTPClient;
public boolean isDirectoryExists(FTPClient ftpClient, String dirPath) throws IOException {
// 保存当前工作目录
String originalPath = ftpClient.printWorkingDirectory();
try {
// 尝试切换到目标目录
return ftpClient.changeWorkingDirectory(dirPath);
} finally {
// 恢复原始工作目录
ftpClient.changeWorkingDirectory(originalPath);
}
}
方法3:使用sendNoOp()方法(更高效)
import org.apache.commons.net.ftp.FTPClient;
public boolean isDirectoryExists(FTPClient ftpClient, String dirPath) throws IOException {
// 保存当前工作目录
String originalPath = ftpClient.printWorkingDirectory();
try {
// 尝试切换到目标目录
if (ftpClient.changeWorkingDirectory(dirPath)) {
return true;
}
// 如果切换失败,检查是否是文件而不是目录
String parentPath = new File(dirPath).getParent();
if (parentPath != null) {
ftpClient.changeWorkingDirectory(parentPath);
String dirName = new File(dirPath).getName();
FTPFile[] files = ftpClient.listFiles(dirName);
return files != null && files.length == 1 && files[0].isDirectory();
}
return false;
} finally {
// 恢复原始工作目录
ftpClient.changeWorkingDirectory(originalPath);
}
}
完整示例
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;
}
// 设置文件传输模式为被动模式
ftpClient.enterLocalPassiveMode();
// 检查目录是否存在
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 originalPath = ftpClient.printWorkingDirectory();
try {
return ftpClient.changeWorkingDirectory(dirPath);
} finally {
ftpClient.changeWorkingDirectory(originalPath);
}
*/
// 方法3: 使用sendNoOp()(推荐)
String originalPath = ftpClient.printWorkingDirectory();
try {
if (ftpClient.changeWorkingDirectory(dirPath)) {
return true;
}
String parentPath = new File(dirPath).getParent();
if (parentPath != null) {
ftpClient.changeWorkingDirectory(parentPath);
String dirName = new File(dirPath).getName();
FTPFile[] files = ftpClient.listFiles(dirName);
return files != null && files.length == 1 && files[0].isDirectory();
}
return false;
} finally {
ftpClient.changeWorkingDirectory(originalPath);
}
}
}
注意事项
-
确保已添加Apache Commons Net库的依赖:
<dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.9.0</version> </dependency> -
方法3(使用
changeWorkingDirectory())是最可靠的方法,因为它直接尝试切换到目标目录。 -
在检查目录是否存在后,记得恢复原始工作目录,以免影响后续操作。
-
对于大型FTP服务器,
listFiles()方法可能会比较慢,因为它需要获取目录内容。
(图片来源网络,侵删) -
确保在操作前正确设置了FTP连接模式(主动或被动)。

