我正在尝试使用Apache Commons API执行复制命令。以下是我的努力:
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);
输出:
scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt"root@my.server.com:/ Leo / Ta / San Diego“
org.apache.commons.exec.ExecuteException:进程退出并显示错误:1(退出值:1)
相同的程序可以正常使用目标文件夹,但没有空格:
String destination =“/ Leo / Ta / SanJose”;
scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt root@my.server.com:/ Leo / Ta / SanJose
答案 0 :(得分:0)
使用CommandLine的addArgument
方法,而不是构造命令字符串。这将确保您的命令在语法上是正确的。
以下代码演示了它:
CommandLine commandLine = new CommandLine("scp");
commandLine.addArgument("-i", false);
commandLine.addArgument(privateKey, false);
commandLine.addArgument(currentFile, false);
commandLine.addArgument(target + destination, false);
答案 1 :(得分:0)
commandLine.addArgument(target + destination,false);
public CommandLine addArguments(String addArguments,boolean handleQuoting)
这有效!!