Java判断FTP文件是否存在
在Java中判断FTP服务器上的文件是否存在,可以使用Apache Commons Net库中的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(String server, int port, String username,
String password, String remoteFilePath) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(username, password);
// 设置文件类型为二进制(可选)
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 进入远程目录
String remoteDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf('/'));
String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf('/') + 1);
if (!remoteDir.isEmpty()) {
ftpClient.changeWorkingDirectory(remoteDir);
}
// 获取文件列表
FTPFile[] files = ftpClient.listFiles(fileName);
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();
}
}
}
使用size()方法
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public boolean isFileExists(String server, int port, String username,
String password, String remoteFilePath) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(username, password);
// 设置文件类型为二进制(可选)
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 检查文件大小
long fileSize = ftpClient.listFiles(remoteFilePath)[0].getSize();
return fileSize >= 0;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用retrieveFile()方法
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public boolean isFileExists(String server, int port, String username,
String password, String remoteFilePath) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(username, password);
// 设置文件类型为二进制(可选)
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 尝试检索文件
InputStream inputStream = ftpClient.retrieveFileStream(remoteFilePath);
boolean success = inputStream != null;
if (success) {
inputStream.close();
}
return success;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意事项
-
依赖库:需要添加Apache Commons Net库的依赖
<dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.9.0</version> </dependency> -
连接模式:根据FTP服务器配置,可能需要设置被动模式
ftpClient.enterLocalPassiveMode();
-
编码问题:如果文件名包含非ASCII字符,可能需要设置编码
ftpClient.setControlEncoding("UTF-8"); -
超时设置:可以设置连接和读取超时
ftpClient.setDefaultTimeout(10000); // 10秒 ftpClient.setConnectTimeout(10000); // 10秒 ftpClient.setDataTimeout(10000); // 10秒
-
异常处理:实际应用中应该更细致地处理各种异常情况
方法都可以用来判断FTP文件是否存在,选择哪种方法取决于你的具体需求和FTP服务器的配置。
