Search code examples
javasmbunknown-host

java.net.UnknownHostException: ..__MSBROWSE__ when using jcifs


I'm trying to copy a file from a local directory to a network share on a windows box. I'm using jcifs 1.3.17. I successfully get a connection to the destination, actually create the file there, get a "canWrite" status of "true", but when I try to copy the contents of the local file to the remote file, I get the following error:

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

. . .

code snippet:

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

I don't get it... if I can create the file on the destination and smb sees that I can write to it, why does the doCopy fail?


Solution

  • I don't get it too but... try this. It works!

        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.
        }
    

    Credits go to this question and answer: How to copy file from smb share to local drive using jcifs in Java?