如何在使用Java的ProcessBuilder调用PSCP时遍历到父目录?

huangapple go评论62阅读模式
英文:

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&lt;String&gt; 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(&quot;C:\Program Files (x86)\PuTTY\pscp&quot; -pw &quot;password&quot; -r folder/file_to_be_transferred.txt &quot;username@hostname:/remote/unix/server/location/folder_name&quot;)

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.

huangapple
  • 本文由 发表于 2020年9月3日 16:31:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63719770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定