6

I need to connect to a UNC "directory" and to create a file in that directory. I found this entry on stackoverflow : access to file using Java with Samba JCIFS. A good thing is that it works well on my system, but when I put the program to the server I get the following exception :

Exception in thread "main" jcifs.smb.SmbException: Failed to connect: <serverName>
jcifs.util.transport.TransportException: Connection in error
jcifs.util.transport.TransportException
java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at jcifs.smb.SmbTransport.ssn139(SmbTransport.java:196)
    at jcifs.smb.SmbTransport.negotiate(SmbTransport.java:249)
    at jcifs.smb.SmbTransport.doConnect(SmbTransport.java:322)
    at jcifs.util.transport.Transport.run(Transport.java:241)
    at java.lang.Thread.run(Unknown Source)
    at jcifs.util.transport.Transport.run(Transport.java:258)
    at java.lang.Thread.run(Unknown Source)
    at jcifs.util.transport.Transport.connect(Transport.java:154)
    at jcifs.smb.SmbTransport.connect(SmbTransport.java:307)
    at jcifs.smb.SmbTree.treeConnect(SmbTree.java:156)
    at jcifs.smb.SmbFile.doConnect(SmbFile.java:911)
    at jcifs.smb.SmbFile.connect(SmbFile.java:954)
    at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
    at jcifs.smb.SmbFile.open0(SmbFile.java:972)
    at jcifs.smb.SmbFile.open(SmbFile.java:1006)
    at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:142)
    at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:97)
    at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:67)
    at path.unc.TestUNC.main(TestUNC.java:79)
    at jcifs.smb.SmbTransport.connect(SmbTransport.java:309)
    at jcifs.smb.SmbTree.treeConnect(SmbTree.java:156)
    at jcifs.smb.SmbFile.doConnect(SmbFile.java:911)
    at jcifs.smb.SmbFile.connect(SmbFile.java:954)
    at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
    at jcifs.smb.SmbFile.open0(SmbFile.java:972)
    at jcifs.smb.SmbFile.open(SmbFile.java:1006)
    at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:142)
    at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:97)
    at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:67)
    at path.unc.TestUNC.main(TestUNC.java:79)

I have created the following code:

    //... read user, pw and uncPath from console
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", user, pw);

    SmbFile dir = new SmbFile(uncPath, auth);

    if (dir.isDirectory()) {
        writer.println(uncPath + " is a directory");
    }

    uncPath = uncPath + "/test.txt";

    writer.println("full path = '" + uncPath + "'");

    SmbFile smbFile = new SmbFile(uncPath, auth);

    writer.println(smbFile.getPermission());

    SmbFileOutputStream uncOut = new SmbFileOutputStream(smbFile);
    PrintWriter uncPrint = new PrintWriter(uncOut);
    uncPrint.println("text from " + TestUNC.class);
    uncPrint.flush();
    // close stream

Which creates the following output on the console:

    smb://<serverName>/myDirectory is a directory
    full path = 'smb://<serverName>/myDirectory/test.txt'

    (java.security.AllPermission <all permissions> <all actions>)

So it can access the directory and also have all permissions.

Like I said, on my local machine it works great (a Win 7 machine). The server seems not able to create the file. But can log in and check if the path is a directory, the server is a Win 2008 machine.

One perhaps interesting point on both machine the command net use I: \\<serverName>\myDirectory <pw> /user:<domain\user> works fine, and allows to create files in there.

My thought was that the response from the server takes too long and jcifs closes it, for that reason I changed the timeout values :

    System.setProperty("jcifs.smb.client.responseTimeout", "120000"); // default: 30000 millisec.
    System.setProperty("jcifs.smb.client.soTimeout", "140000"); // default: 35000 millisec.
Community
  • 1
  • 1
user2680083
  • 125
  • 1
  • 2
  • 10
  • We didn't solved the problem. We did follwoing "workaround": On the server we made a request to get a permanent mount via net use. – user2680083 Apr 15 '15 at 20:48
  • Did you check if the target windows share is a DFS share. If yes try to use the resolved/physical server name that hosts the files/diretory ... – Georg Aug 03 '16 at 13:50
  • @ron190 I backed out the spaces you added before colon. Colon obeys the same spacing rules in English as other punctuation; no space before, one space after. – tripleee Aug 29 '16 at 10:02
  • Did you check that the Server support SMB1? – Eliad Cohen May 15 '18 at 14:13

2 Answers2

2

Try using IP address instead of server name. I had the same issue and got fixed using IP address

full path = 'smb://<IPaddress>/myDirectory/test.txt'
Nayana Priyankara
  • 1,392
  • 1
  • 18
  • 26
0

Recently I had similar issue and i found the issue while connecting to the SMB server is because of below reason -

enter image description here

Note, worth checking whether its an issue with the DNS/IP also.

Sanjeet Pandey
  • 546
  • 4
  • 23
  • BTW there are several SMB client written in Java that support SMB2 and above – Eliad Cohen May 15 '18 at 14:14
  • Thank you - I have resolved my connectivity issue by enabling the below setting in the windows 10 OS - Open Control Panel (just start typing Control in the search box to find its shortcut quickly). Click Programs, and then click Turn Windows features on or off (under the Programs heading). Clear the check box for SMB 1.0/CIFS File Sharing Support – Sanjeet Pandey May 16 '18 at 06:21