简体   繁体   中英

Can't connect to samba server with jcifs

I am trying to copy some files from my local machine to a samba server connected in the same network as my computer with Java.

If I try to login via browser or open it in the Ubuntu network devices folder there is no problem, however via Java I don't seem to be able to successfully login.

Here is my code:

public static void function() throws MalformedURLException, SmbException {
    String urlToBackUpFile = "smb://dlink-5e6490//Volume_1/newfolder/file.zip";
    NtlmPasswordAuthentication auth = new **NtlmPasswordAuthentication("192.168.1.8", "admin", null);**
    SmbFile dir = new SmbFile(urlToBackUpFile, auth);
    SmbFile dest = new SmbFile("file:///home/username/file.zip");
    dir.copyTo(dest);
}

I always get one of two errors.

If I only insert the domain and null in the username and password fields, I get this error:

Exception in thread "main" jcifs.smb.SmbException: The network name cannot be found. at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:563) at jcifs.smb.SmbTransport.send(SmbTransport.java:663) at jcifs.smb.SmbSession.send(SmbSession.java:238) at jcifs.smb.SmbTree.treeConnect(SmbTree.java:176) at jcifs.smb.SmbFile.doConnect(SmbFile.java:911) at jcifs.smb.SmbFile.connect(SmbFile.java:957) at jcifs.smb.SmbFile.connect0(SmbFile.java:880) at jcifs.smb.SmbFile.copyTo(SmbFile.java:2304) at javaapplication2.JavaApplication2.function(JavaApplication2.java:40) at javaapplication2.JavaApplication2.main(JavaApplication2.java:48) Java Result: 1

However, if I try to insert the values "admin" to the username and null or "" to the password (there is no password for the admin account) it gives me this error:

Exception in thread "main" jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password. at jcifs.smb.SmbSession.sessionSetup(SmbSession.java:406) at jcifs.smb.SmbSession.send(SmbSession.java:218) at jcifs.smb.SmbTree.treeConnect(SmbTree.java:176) at jcifs.smb.SmbFile.doConnect(SmbFile.java:911) at jcifs.smb.SmbFile.connect(SmbFile.java:957) at jcifs.smb.SmbFile.connect0(SmbFile.java:880) at jcifs.smb.SmbFile.copyTo(SmbFile.java:2303) at javaapplication2.JavaApplication2.function(JavaApplication2.java:40) at javaapplication2.JavaApplication2.main(JavaApplication2.java:48) Java Result: 1

I tried all kinds of combinations in the NtlmPasswordAuthentication line, such like...

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("192.168.1.8", null, null);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("192.168.1.8", "admin", null);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("192.168.1.8", "admin", "");
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("smb://dlink-5e6490/", null, null);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("smb://dlink-5e6490/", "admin", null);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("smb://dlink-5e6490/", "admin", "");

...but nothing seems to work.

Does anyone know where can the problem be?

I ended up using Linux commands in order to accomplish this task:

String command = "mount -o password=\"" + serverPassword + "\" -t cifs //192.168.1.8/Volume_1 /mnt/files/";

String[] cmd = {"/bin/bash", "-c", ("echo " + sudoPassword + "| sudo -S " + command)};

Runtime.getRuntime().exec(cmd);

Using sudo this way is not advised, but was the only way I managed to make this to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM