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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With