ftp服务器上目录文件是否存在,在 Java 中如何检查 FTP 服务器上的文件或目录是否存在...

有的时候,在执行进一步操作之前,需要知道 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();

}

}

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值