Java 检查远程计算机上是否存在目录
如何检查远程计算机上是否存在目录 我想在远程计算机上创建目录。为此,我需要检查远程计算机上是否存在该目录。java中有什么方法可以检查这一点吗 我想要这样的东西Java 检查远程计算机上是否存在目录,java,filesystems,Java,Filesystems,如何检查远程计算机上是否存在目录 我想在远程计算机上创建目录。为此,我需要检查远程计算机上是否存在该目录。java中有什么方法可以检查这一点吗 我想要这样的东西 File f = new File("http:/sac/svn/MockRepo/Mockrepo/branches/eQubeQa/Definitions/sac_cat/") if(!f.exists()){ //create directory } 我知道文件类中有exist方法。 它与本地文件系统一起工作。 但对于远
File f = new File("http:/sac/svn/MockRepo/Mockrepo/branches/eQubeQa/Definitions/sac_cat/")
if(!f.exists()){
//create directory
}
我知道文件类中有exist方法。
它与本地文件系统一起工作。
但对于远程目录,它总是返回false
请给我示例代码。由于
权限问题,您可能无法直接访问远程计算机中的文件夹 如果您有适当的权限,您可以使用JSch库通过ssh连接到该机器(假设您有权限),然后执行一组命令来确定目录是否存在如果您可以访问远程机器上的FTP服务器,您可以尝试通过FTP连接并检测目录是否存在
下面是一个示例代码:
package net.codejava.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.
* @author www.codejava.net
*
*/
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;
}
/**
* 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";
FTPCheckFileExists ftpApp = new FTPCheckFileExists();
try {
ftpApp.connect(hostname, port, username, password);
boolean exist = ftpApp.checkDirectoryExists(dirPath);
System.out.println("Is directory " + dirPath + " exists? " + exist);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
ftpApp.logout();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
来源:1)请对HTML或XML等代码、输入/输出和结构化文档使用代码格式。为此,请选择示例并单击消息发布/编辑表单上方的{}
按钮。2) 如何在远程计算机上创建目录?3) “请给我示例代码。”事实并非如此。您的java程序在哪个操作系统上运行?什么是目标操作系统?我的代码在Windows上运行OS@user2745675远程机器的操作系统是什么?我拥有所有权限我拥有所有必需的权限。您能给我一个示例代码吗?您需要ssh访问远程机器。我在下面的import org.apache.commons.net.ftp.ftp.FTPClient行中遇到错误;我想我需要加一些罐子。您能告诉我需要在类路径中添加哪个jar吗?