英文:
How can I traverse to parent directory when calling PSCP using Java's ProcessBuilder?
问题
我正在使用以下Java代码从Java中调用pscp:
public static ArrayList<String> runWindowsCommand(String... args) throws WindowsCmdFault {
try {
ProcessBuilder pb = new ProcessBuilder(args);
Process p = pb.start();
//...
//... 用于获取输出并将其返回的代码
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
我以以下方式调用此函数:
runWindowsCommand("C:\\Program Files (x86)\\PuTTY\\pscp -pw \"password\" -r folder/file_to_be_transferred.txt \"username@hostname:/remote/unix/server/location/folder_name\"")
这会成功执行,并显示以下输出:
Arraylist returned (output) =
[, file_to_be_transferred.txt | 3 kB | 2.7 kB/s | ETA: 00:00:00 | 100%]
看起来传输成功了。但是,当我通过putty登录到远程Unix服务器并检查该文件时,显示**未更新任何内容**。
文件权限已设置为**666**。
当我在**命令提示符**上执行相同的命令(我们发送给函数的命令)时,它会给出相同的输出,并且文件实际上会被传输。
但是,当我通过上面给出的Java代码运行此命令时,文件实际上并没有被传输。
为什么PSCP不传输文件?
**更新:经过进一步的调查,特别感谢@Martin Prikryl,我能够将此问题进一步缩小到了根本原因。**
**根本原因:**
问题在于当从PSCP建立用户连接时,默认连接到
`(root)/home/username`。
我们需要遍历的目录位于
(root)/www/....lengthy/folder/here。
**余下的问题:**
我正在尝试在连接后向上遍历文件夹。我试图在提供实际路径之前,添加两个连续的句点,以便向上遍历到根目录。但这不起作用。
我正在尝试将远程主机名设置为pscp:
../../www/..lengthy/folder/here
但是通过Java执行时,由于某种奇怪的原因,这会失败,但在命令提示符中运行却有效。
我如何在实际路径之前添加包含父级的远程路径?
英文:
I'm calling pscp from Java using the following code:
public static ArrayList<String> runWindowsCommand(String... args) throws WindowsCmdFault {
try {
ProcessBuilder pb = new ProcessBuilder(args);
Process p = pb.start();
//...
//... code to fetch the output and return it back
}catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
I call this function as follows:
runWindowsCommand("C:\Program Files (x86)\PuTTY\pscp" -pw "password" -r folder/file_to_be_transferred.txt "username@hostname:/remote/unix/server/location/folder_name")
This executes fine and shows the following output:
Arraylist returned (output) =
[, file_to_be_transferred.txt | 3 kB | 2.7 kB/s | ETA: 00:00:00 | 100%]
It appears that the transfer was successful. Except that when I login to the remote unix server via putty and check that file, it shows that nothing was updated.
The file's permission has been set to 666.
When I execute this same command (that we're sending to the function) directly on the cmd prompt, then it gives the same output and the file is actually transferred.
When I run this same command via the Java code given above the file isn't actually transferred.
Why is PSCP not transferring the file?
Update:On further investigation and a big thanks to @Martin Prikryl, I was able to narrow down this issue to the root cause.
Root cause:
The problem is that when the user connection is established from the PSCP, then it connects by default to
(root)/home/username
.
The directory that we need to traverse to lies in
(root)/www/....lengthy/folder/here .
Remaining problem:
I'm trying to traverse upwards through the folders after connecting. I'm trying to add a double-period in order traverse upwards towards the root before supplying my path. But it doesn't work.
I'm trying this with pscp as the remote hostname:
../../www/..lengthy/folder/here
but this fails for some odd reason when executed through Java but works through cmd prompt.
How can I add a remote path that includes the second parent before including my actual path?
答案1
得分: -1
似乎在使用Java + PSCP时存在一些限制。我通过将文件上传到默认目录来解决了这个问题。然后,我通过Putty的命令行建立连接,并将文件传输到所需目的地。我将我的答案放在这里。
英文:
It seems like there's some limitation when you use Java + PSCP. I solved this problem by uploading the file to a default directory. After that I make a connection via putty's command-line and transfer the file to the required destination. I'm placing my answer over here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论