简体   繁体   English

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

[英]How can I traverse to parent directory when calling PSCP using Java's ProcessBuilder?

I'm calling pscp from Java using the following code:我正在使用以下代码从 Java 调用 pscp:

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.除了当我通过 putty 登录到远程 unix 服务器并检查该文件时,它显示没有任何更新。

The file's permission has been set to 666.该文件的权限已设置为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.当我直接在cmd 提示符下执行这个相同的命令(我们发送给函数)时,它会给出相同的输出并且文件被实际传输。

When I run this same command via the Java code given above the file isn't actually transferred.当我通过上面给出的 Java 代码运行相同的命令时,文件实际上并未传输。

Why is PSCP not transferring the file?为什么 PSCP 不传输文件?

Update:On further investigation and a big thanks to @Martin Prikryl, I was able to narrow down this issue to the root cause.更新:经过进一步调查,非常感谢@Martin Prikryl,我能够将这个问题缩小到根本原因。

Root cause: The problem is that when the user connection is established from the PSCP, then it connects by default to (root)/home/username .根本原因:问题是当从 PSCP 建立用户连接时,它默认连接到(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:我正在尝试使用 pscp 作为远程主机名:

../../www/..lengthy/folder/here 

but this fails for some odd reason when executed through Java but works through cmd prompt.但是当通过 Java 执行但通过 cmd 提示符工作时,这会由于某种奇怪的原因而失败。

How can I add a remote path that includes the second parent before including my actual path?如何在包含我的实际路径之前添加包含第二个父级的远程路径?

It seems like there's some limitation when you use Java + PSCP.使用 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.之后,我通过 putty 的命令行建立连接并将文件传输到所需的目的地。 I'm placing my answer over here.我把我的答案放在这里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM