使用运行时从Java运行smbclient命令

问题描述 投票:0回答:1

我正在尝试运行smbclient命令,使用带有以下代码的Java Runtime类将文件复制到共享驱动器:

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个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.