【问题标题】:Running smbclient command from Java using Runtime使用 Runtime 从 Java 运行 smbclient 命令
【发布时间】: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


    【解决方案1】:

    我将该驱动器安装到我的机器中并将文件复制到该安装目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-24
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多