2

我正在尝试将文件从本地目录复制到 windows 框上的网络共享。我正在使用 jcifs 1.3.17。我成功连接到目的地,实际上在那里创建了文件,获得了“true”的“canWrite”状态,但是当我尝试将本地文件的内容复制到远程文件时,出现以下错误:

jcifs.smb.SmbException: Failed to connect to server
java.net.UnknownHostException: ..__MSBROWSE__.<01>
   at jcifs.netbios.NbtAddress.doNameQuery(NbtAddress.java:317)

. . .

代码片段:

        SmbFile source = new SmbFile(original);
        SmbFile dest = new SmbFile (target,auth);
        dest.createNewFile();
        boolean canWrite = dest.canWrite();
        source.copyTo(dest);

我不明白...如果我可以在目标上创建文件并且 smb 看到我可以写入它,为什么doCopy会失败?

4

1 回答 1

2

我也不明白,但是……试试这个。有用!

    String source = "smb://SERVER/PATH/FILE";
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, "USERNAME", "PASSWORD");
    SmbFile sourceFile = new SmbFile(source, auth);
    String destination = "LOCAL_PATH_TO_FILE";

    byte[] buffer;
    int length;

    try {
        FileOutputStream fileOutputStream = new FileOutputStream(destination);
        InputStream fileInputStream = sourceFile.getInputStream();
        try {
            buffer = new byte[16 * 1024 * 1024];
            while ((length = fileInputStream.read(buffer)) > 0) {
                fileOutputStream.write(buffer, 0, length);
            }
        } finally {
            fileInputStream.close();
            fileOutputStream.close();
        }
    } catch (SmbException e) {
        // Error handling.    
    } catch (FileNotFoundException e) {
        // Error handling.
    } catch (IOException e) {
        // Error handling.
    }

学分转到这个问题和答案:如何使用 Java 中的 jcifs 将文件从 smb 共享复制到本地驱动器?

于 2015-07-15T10:16:34.670 回答