gpt4 book ai didi

带有 scp 命令的 Java 运行时 exec

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:32:18 27 4
gpt4 key购买 nike

这是我将文件从本地复制到远程机器的代码的一部分

try {
Process cpyFileLocal = Runtime.getRuntime().exec("scp " + rFile+"*.csv" + " root@" + host + ":" + lFile);
InputStream stderr = cpyFileLocal.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<ERROR>");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("</ERROR>");
int exitVal = cpyFileLocal.waitFor();
System.out.println("Process exitValue: " + exitVal);
System.out.println("...." + cpyFileLocal.exitValue());
System.out.println("SCP COMMAND "+"scp "+rFile+"*.csv" +" root@"+host+":"+lFile);
System.out.println("Sending complete...");
} catch (Exception ex) {
ex.printStackTrace();
}

输出是...

<ERROR>

/opt/jrms/rmsweb/transfer/cn00/outgoing/*.csv: No such file or directory
</ERROR>

Process exitValue: 1

....1

SCP COMMAND scp /opt/jrms/rmsweb/transfer/cn00/outgoing/*.csv root@10.50.1.29:/opt/jrms/transfer/incoming/

但是当我在本地机器的终端中运行命令时,它工作正常当我运行 ll 时,文件就在那里

-rwxr-xr-x 1 freddie freddie 140 Apr 22 09:13 gc00cn00150420092629.csv*

-rwxr-xr-x 1 freddie freddie 105 Apr 22 09:13 gc00cn00150420122656.csv*

请帮忙

最佳答案

如果您使用的是 java 7 及更高版本,您应该使用 ProcessBuilder 而不是 Runtime.getRuntime().exec() 并且在 ProcessBuilder 你可以指定执行目录:

 ProcessBuilder pb = new ProcessBuilder("scp", rFile+"*.csv", "root@" + host + ":" + lFile);
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory("directory where the csv files located");
Process p = pb.start();

关于带有 scp 命令的 Java 运行时 exec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29787186/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com