我需要连接到UNC“目录”并在该目录中创建文件。我在stackoverflow上找到了此条目:access to file using Java with Samba JCIFS。一件好事是它在我的系统上运行良好,但是当我将该程序放到服务器上时,出现以下异常:
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)
我创建了以下代码:
//... 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
在控制台上创建以下输出:
smb://<serverName>/myDirectory is a directory
full path = 'smb://<serverName>/myDirectory/test.txt'
(java.security.AllPermission <all permissions> <all actions>)
因此,它可以访问目录并具有所有权限。
就像我说的那样,在我的本地计算机上运行良好(Win 7计算机)。服务器似乎无法创建文件。但是可以登录并检查路径是否为目录,服务器是否为Win 2008计算机。
在两台机器上,也许有趣的一点是,命令
net use I: \\<serverName>\myDirectory <pw> /user:<domain\user>
可以正常工作,并允许在其中创建文件。我的想法是服务器的响应时间太长,jcifs将其关闭,因此我更改了超时值:
System.setProperty("jcifs.smb.client.responseTimeout", "120000"); // default: 30000 millisec.
System.setProperty("jcifs.smb.client.soTimeout", "140000"); // default: 35000 millisec.
最佳答案
尝试使用IP地址而不是服务器名称。我遇到了同样的问题,并使用IP地址进行了修复
full path = 'smb://<IPaddress>/myDirectory/test.txt'