Java JCIFS - 连接中断
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24912430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JCIFS - connection breaks
提问by user2680083
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 :
我需要连接到 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)
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.
就像我说的,在我的本地机器上它运行良好(Win 7 机器)。服务器似乎无法创建文件。但是可以登录查看路径是否是目录,服务器是Win 2008机器。
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.
在两台机器上可能有趣的一点是该命令net use I: \\<serverName>\myDirectory <pw> /user:<domain\user>
工作正常,并允许在其中创建文件。
My thought was that the response from the server takes too long and jcifs closes it, for that reason I changed the timeout values :
我的想法是来自服务器的响应时间太长,jcifs 将其关闭,因此我更改了超时值:
System.setProperty("jcifs.smb.client.responseTimeout", "120000"); // default: 30000 millisec.
System.setProperty("jcifs.smb.client.soTimeout", "140000"); // default: 35000 millisec.
回答by Nayana Priyankara
Try using IP address instead of server name. I had the same issue and got fixed using IP address
尝试使用 IP 地址而不是服务器名称。我遇到了同样的问题并使用 IP 地址修复了
full path = 'smb://<IPaddress>/myDirectory/test.txt'