有的时候,在执行进一步操作之前,需要知道 FTP 服务器上是否存在指定的目录或文件。 本文介绍了如何使用 Apache Commons Net 库实现这个功能。
要检测目录或文件是否存在,可以检查服务器的响应代码。 根据 FTP 协议规范,当请求的文件或目录不可用时,FTP 服务器返回代码 550。 因此要检查的代码如下所示:
// invokes an operation for a file/diretory...
// checks reply code:
int returnCode = ftpClient.getReplyCode();
if (returnCode == 550) {
// file/directory is unavailable
}
判断目录是否存在:
要判断指定的目录是否存在,需要以下两步:
将工作目录切换为该目录。
检查来自服务器的响应代码。
下面是示例代码:
boolean checkDirectoryExists(String dirPath) throws IOException {
ftpClient.changeWorkingDirectory(dirPath);
returnCode = ftpClient.getReplyCode();
if (returnCode == 550) {
return false;
}
return true;
}
注意:如果目录确实存在,那么工作目录切换后最好再切换回原来的目录,否则将会更改工作目录。
判断文件是否存在:
要判断指定的文件是否存在,需要以下两步:
尝试检索该文件输入流。
检查来自服务器的响应代码。
下面是示例代码:
boolean checkFileExists(String filePath) throws IOException {
InputStream inputStream = ftpClient.retrieveFileStream(filePath);
returnCode = ftpClient.getReplyCode();
if (inputStream == null || returnCode == 550) {
return false;
}
return true;
}
判断 FTP 服务器上文件/目录是否存在的完整 Java 示例:
为了演示,下面创建一个示例程序,该程序登录到 FTP 服务器,然后检查目录和文件的存在,最后从服务器注销登录:
package org.91tech.ftp;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
/**
* This program demonstrates how to determine existence of a specific
* file/directory on a remote FTP server.
*
*/
public class FTPCheckFileExists {
private FTPClient ftpClient;
private int returnCode;
/**
* Determines whether a directory exists or not
* @param dirPath
* @return true if exists, false otherwise
* @throws IOException thrown if any I/O error occurred.
*/
boolean checkDirectoryExists(String dirPath) throws IOException {
ftpClient.changeWorkingDirectory(dirPath);
returnCode = ftpClient.getReplyCode();
if (returnCode == 550) {
return false;
}
return true;
}
/**
* Determines whether a file exists or not
* @param filePath
* @return true if exists, false otherwise
* @throws IOException thrown if any I/O error occurred.
*/
boolean checkFileExists(String filePath) throws IOException {
InputStream inputStream = ftpClient.retrieveFileStream(filePath);
returnCode = ftpClient.getReplyCode();
if (inputStream == null || returnCode == 550) {
return false;
}
return true;
}
/**
* Connects to a remote FTP server
*/
void connect(String hostname, int port, String username, String password)
throws SocketException, IOException {
ftpClient = new FTPClient();
ftpClient.connect(hostname, port);
returnCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(returnCode)) {
throw new IOException("Could not connect");
}
boolean loggedIn = ftpClient.login(username, password);
if (!loggedIn) {
throw new IOException("Could not login");
}
System.out.println("Connected and logged in.");
}
/**
* Logs out and disconnects from the server
*/
void logout() throws IOException {
if (ftpClient != null && ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
System.out.println("Logged out");
}
}
/**
* Runs this program
*/
public static void main(String[] args) {
String hostname = "www.yourserver.com";
int port = 21;
String username = "your_user";
String password = "your_password";
String dirPath = "Photo";
String filePath = "Music.mp4";
FTPCheckFileExists ftpApp = new FTPCheckFileExists();
try {
ftpApp.connect(hostname, port, username, password);
boolean exist = ftpApp.checkDirectoryExists(dirPath);
System.out.println("Is directory " + dirPath + " exists? " + exist);
exist = ftpApp.checkFileExists(filePath);
System.out.println("Is file " + filePath + " exists? " + exist);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
ftpApp.logout();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}