简体   繁体   English

如何正确使用 smbj 连接和列出 Android java 中的 samba 共享上的文件?

[英]How to properly use smbj to connect and list files on a samba share in Android java?

When I connect with smbj, error log shows:当我连接 smbj 时,错误日志显示:

...
I/c.h.s.c.Connection: Successfully authenticated user on 192.168.1.222, session is 4399187361905
I/c.h.s.s.Session: Logging off session 4399187361905 from host 192.168.1.222
I/c.h.s.t.PacketReader: Thread[Packet Reader for 192.168.1.222,5,main] stopped.
I/c.h.s.c.Connection: Closed connection to 192.168.1.222
I/c.h.s.s.Session: Connecting to \\192.168.1.222\pop on session 4399187361905

Immediately - without any delay.立即- 没有任何延迟。 So the connection is closed immediately after it is opened and it will them crash if I try to list files...所以连接在打开后立即关闭,如果我尝试列出文件,它们会崩溃......

Caused by: com.hierynomus.smbj.common.SMBRuntimeException: com.hierynomus.protocol.transport.TransportException: Cannot write SMB2_TREE_CONNECT with message id << 4 >> as transport is disconnected

Which seems obvious since there's no open connection.这似乎很明显,因为没有打开的连接。

A question, related to smbj, indicated there was a problem with the way that person used the try statement... I believe this is a similar case.一个与 smbj 相关的问题表明该人使用 try 语句的方式存在问题......我相信这是一个类似的案例。

Within an AsyncTask, I have:在 AsyncTask 中,我有:

try (Connection connection = client.connect(serverName)) {
    AuthenticationContext ac = new AuthenticationContext(username, password.toCharArray(), domain);
    Session session = connection.authenticate(ac);
    this.session = session;
    return session;
} catch (IOException e) {
    e.printStackTrace();
}

I'm certain there's a problem with the try-catch.我确定 try-catch 有问题。 Can someone please give a complete piece of code including the AsyncTask - as the smbj module github should have.有人可以提供一段完整的代码,包括 AsyncTask - 作为 smbj 模块 github 应该有。 I hope this will solve most issues for all users.我希望这将解决所有用户的大多数问题。

You are using a try-with resource block.您正在使用 try-with 资源块。 A try-with-resource block automatically executes close() on the variable that has been initialized in it's head (the head is Connection connection = client.connect(serverName) ). try-with-resource 块会自动对已在其头部初始化的变量执行close() (头部是Connection connection = client.connect(serverName) )。 In your case it is calling connection.close() when its is reaching the end of the try block when it reaches the return statement.在您的情况下,它在到达return语句时到达try块的末尾时调用connection.close()

If you want to make the connection persistent move the initialization block inside the try-catch block:如果要使连接持久,请移动 try-catch 块内的初始化块:

try {
    Connection connection = client.connect(serverName);
    this.connection = connection; // save the connection reference to be able to close it later manually
    AuthenticationContext ac = new AuthenticationContext(username, password.toCharArray(), domain);
    Session session = connection.authenticate(ac);
    this.session = session;
    return session;
} catch (IOException e) {
    e.printStackTrace();
}

Of course you should not forget to manually call connection.close() when you want to close the connection.当然,当你想关闭连接时,你不应该忘记手动调用connection.close() Therefore you should also save the reference to the created connection somewhere, not only the session.因此,您还应该在某处保存对已创建连接的引用,而不仅仅是 session。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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