如何使用Java代码获取服务器上的文件路径?
我已在服务器“www.server.com”上上载了一个文件。现在我想通过java代码知道同一服务器上文件的路径。是否有任何具体的方法或流程?我是新来的如何使用Java代码获取服务器上的文件路径?,java,file,server,ftp,Java,File,Server,Ftp,我已在服务器“www.server.com”上上载了一个文件。现在我想通过java代码知道同一服务器上文件的路径。是否有任何具体的方法或流程?我是新来的 public class Test { public static void main(String[] args) { test test = new test(); String server ="www.server.com"; int port = 21; S
public class Test {
public static void main(String[] args) {
test test = new test();
String server ="www.server.com";
int port = 21;
String username = "abc";
String password = "abc`enter code here`";
FTPClient ftpclient = new FTPClient();
try {
ftpclient.connect(server, port);
ftpclient.login(username, password);
ftpclient.enterLocalPassiveMode();
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
File firstLocalFile = new File("D:\\ADF\\Tax Files\\TERData.zip");
String firstRemoteFile = "TERData.zip";
InputStream inputstream = new FileInputStream(firstLocalFile);
System.out.println("Start Uploading the First File");
System.out.println(" File Path ":+firstLocalFile.getAbsolutePath());
boolean done = ftpclient.storeFile(firstRemoteFile, inputstream);
if(done){
System.out.println(" File successfully uploaded ");
}
} catch (IOException e) {
// TODO: Add catch code
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
你能试着打印
ftpclient.printWorkingDirectory()吗代码>我尝试使用它打印目录,但实际上它不是我需要的目录。哪个目录打印了???问题是当我通过端口22登录时,我看到了我想要的目录。但是当我通过端口21登录时,该目录不存在。但文件存在。上面的方法调用为我提供了当我通过我不想要的端口21登录时。你能帮我吗?我想telnet使用22端口。21用于FTP。通过打印remoteFile1,我将获得服务器上上载文件的路径吗?不,这是一个示例。如果您看到ftpclient返回一个文件对象,那么您也可以获取路径。请浏览ftpfile对象。我将更新我的ans
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
FTPFile[] files1 = ftpClient.listFiles("/test");//"/test/test.txt";
String remoteFile1 =files1[0].getName();
File downloadFile1 = new File("D:/Downloads/test.mp4");
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
}