Unable to read file from shared drive using jcifs api in java

1.9k views Asked by At

I am trying to read file from shared drive but I am getting an error

jcifs.smb.SmbException: Failed to connect to server java.net.UnknownHostException: ..MSBROWSE.<01>

i have tired few things but nothing seems to be working. I am getting System.out.println(sFile.canRead()) this as true, that means connection is successful.

public void readFilefromSharedDrive() throws IOException {
        String user = "user" + ":" + "pwd";
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
        SmbFile sFile = null;
        try {
            sFile = new SmbFile("smb:///ip//path//filname.txt", auth);
            System.out.println(sFile.canRead());
            byte[] buffer = new byte[1024];
            try (SmbFileInputStream in = new SmbFileInputStream(sFile)) {
                int bytesRead = 0;
                do {
                    bytesRead = in.read(buffer);
                    // here you have "bytesRead" in buffer array
                } while (bytesRead > 0);

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "ERROR: " + e);
            }
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }
    }

it throws error at SmbFileInputStream in = new SmbFileInputStream(sFile)

jcifs.smb.SmbException: Failed to connect to server
java.net.UnknownHostException: ..__MSBROWSE__.<01>
    at jcifs.netbios.NbtAddress.doNameQuery(NbtAddress.java:317)
    at jcifs.netbios.NbtAddress.getByName(NbtAddress.java:422)
    at jcifs.netbios.NbtAddress.getByName(NbtAddress.java:403)
    at jcifs.smb.SmbFile.getFirstAddress(SmbFile.java:845)
    at jcifs.smb.SmbFile.connect(SmbFile.java:946)
    at jcifs.smb.SmbFile.connect0(SmbFile.java:875)
    at jcifs.smb.SmbFile.open0(SmbFile.java:965)
    at jcifs.smb.SmbFile.open(SmbFile.java:999)
    at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73)
    at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:65)
    at jcifs.smb.SmbFile.getInputStream(SmbFile.java:2833)
    at java.net.URLConnection.getContent(URLConnection.java:739)
    at 
1

There are 1 answers

0
Theo On

this works for me

 SmbFile smbfile = getSmbFile(newpath, getCredentials(user, password));

     public NtlmPasswordAuthentication getCredentials(String usr, String pwd) {
    NtlmPasswordAuthentication credentials = null;
    if(usr.length()==0){
        credentials = NtlmPasswordAuthentication.ANONYMOUS;
    }else {
        credentials = new NtlmPasswordAuthentication(null, usr,
                pwd);
    }
    return credentials;
}