【发布时间】:2017-06-07 15:13:12
【问题描述】:
我已在服务器“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;
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。