【发布时间】:2015-03-27 12:34:33
【问题描述】:
我正在尝试使用 Java Runtime 类运行 smbclient 命令以将文件复制到共享驱动器,代码如下:
private void copyFiles(String filePath) throws Exception {
String command = "smbclient -A smbclient_authentication.txt //192.14.34.118/testbakup -c \"put " + filePath + "\"";
System.out.println("Smbclinet command:" + command);
Process p = Runtime.getRuntime().exec(command);
int waitFor = p.waitFor();
if (waitFor == 0) {
System.out.println(p.exitValue());
StringBuffer sb = new StringBuffer();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
if (reader != null) reader.close();
} else {
InputStream errorStream = p.getErrorStream();
byte[] buffer = new byte[errorStream.available()];
errorStream.read(buffer);
String str = new String(buffer);
System.out.println(str);
if (errorStream != null) errorStream.close();
}
}
我尝试使用 JCIFS 库,但复制文件需要太多时间。所以我想使用 Java 运行上述命令。我能够从外部运行相同的命令,但不能从 Java 运行,它甚至没有给出任何错误。
【问题讨论】:
标签: java linux runtime.exec processbuilder samba