简体   繁体   English

使用Apache Commons Exec复制

[英]Copy using Apache Commons Exec

I am trying to execute a copy command using Apache Commons API. 我正在尝试使用Apache Commons API执行复制命令。 Below is my effort : 以下是我的努力:

   String privateKey = "/Users/TD/.ssh/id_rsa";
    String currentFile = "/Users/TD/One.txt";
    String target = "root@my.server.com:";

    // Space is present in destination
    String destination="/Leo/Ta/San Diego";

    CommandLine commandLine = new CommandLine("scp");
    commandLine.addArgument("-i");
    commandLine.addArgument(privateKey);
    commandLine.addArgument(currentFile);
    commandLine.addArgument(target + destination);

    System.out.println(commandLine.toString());
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    executor.execute(commandLine);

Output : 输出:

scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt "root@my.server.com:/Leo/Ta/San Diego" scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt“ root@my.server.com:/ Leo / Ta / San Diego”

org.apache.commons.exec.ExecuteException: Process exited with an error: 1(Exit value: 1) org.apache.commons.exec.ExecuteException:进程退出,错误为:1(退出值:1)

Same program works fine with destination folder with no space in it: 同一程序在目标文件夹中没有空格的情况下也可以正常工作:

String destination="/Leo/Ta/SanJose"; 字符串destination =“ / Leo / Ta / SanJose”;

scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt root@my.server.com:/Leo/Ta/SanJose scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt root@my.server.com:/ Leo / Ta / SanJose

Instead of constructing the command string, use CommandLine's addArgument method. 代替构造命令字符串,使用CommandLine的addArgument方法。 That will ensure that your command is syntactically correct. 这样可以确保您的命令在语法上正确。

The following code demonstrates it: 以下代码对此进行了演示:

    CommandLine commandLine = new CommandLine("scp");
    commandLine.addArgument("-i", false);
    commandLine.addArgument(privateKey, false);
    commandLine.addArgument(currentFile, false);
    commandLine.addArgument(target + destination, false);
commandLine.addArgument(target + destination,false);

public CommandLine addArguments(String addArguments, boolean handleQuoting) 公共命令行addArguments(String addArguments,boolean handleQuoting)

this worked !! 这工作了!

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

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