首页 > 解决方案 > SMBJ 抛出传输/eof 异常

问题描述

我正在利用 SMBJ 读取 sbm 文件,虽然一切正常,但它正在抛出,

com.hierynomus.protocol.transport.TransportException: java.io.EOFException: EOF while reading packet

我不明白为什么,它似乎正在正确关闭所有内容。我将以下代码与资源一起使用:

  try (File f = Smb.open(filename)) {
   do my work with the file
  }

当我处理完文件后,我调用了 releaseShare 方法,我看到很多注销会话,并在日志中注销嵌套会话消息.. 然而,这似乎并不重要,它看起来仍然像图书馆认为会话/共享仍处于打开状态,当它在 10 或 15 分钟后对其进行 ping 操作时,会引发异常......这似乎并没有伤害任何东西,就程序的工作而言,但我想摆脱错误...我没有正确关闭/处理什么?

public static DiskShare getShare() throws Exception
{

    if(share == null){
        SmbConfig cfg = SmbConfig.builder().withDfsEnabled(true).build();
        SMBClient client = new SMBClient(cfg);
        Log.info("CREATE SHARE");
        connection = client.connect(sambaIP);
        session = connection.authenticate(new AuthenticationContext(sambaUsername, sambaPass.toCharArray(), sambaDomain));
        share = (DiskShare) session.connectShare(sambaSharedName);


    }
    return(share);

}

public static File open(String filename) throws Exception{
    getShare();
    Set<SMB2ShareAccess> s = new HashSet<>();
    s.add(SMB2ShareAccess.ALL.iterator().next());
    filename = filename.replace("//path base to ignore/","");
    return(share.openFile(filename, EnumSet.of(AccessMask.GENERIC_READ), null, s,  SMB2CreateDisposition.FILE_OPEN, null));
}

public static void releaseShare() throws Exception{

    share.close();
    session.close();
    connection.close();
    share = null;
    session = null;
    connection = null;

}

标签: javasmbmicrosoft-distributed-file-systemsmbj

解决方案


SmbClient 本身也是Closeable. 不要忘记关闭它以确保没有任何资源处于打开状态。


推荐阅读