通过 smbj 获取准确的文件大小

问题描述 投票:0回答:1

我正在尝试使用 smbj 库从 SMB 共享读取文件。 我的代码需要读入字节数组,并且在读取之前我需要知道文件大小。不幸的是,在研究代码时,我只能找到获取分配大小的方法,该大小通常大于实际文件大小。

那么如何获取文件的实际大小(以字节为单位)?

我的(错误代码)到目前为止看起来像这样:

    SMBClient client = new SMBClient();
    try(Connection connection = client.connect(hostname)) {
      AuthenticationContext ac = new AuthenticationContext(user, password.toCharArray(), domain);
      Session session = connection.authenticate(ac);

      // Connect to Share
      try (DiskShare share = (DiskShare) session.connectShare(shareName)) {
          FileAllInformation fileInfo = share.getFileInformation(path);
          File file = share.openFile(path, EnumSet.of(AccessMask.GENERIC_READ), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, null);

          long fileSize = fileInfo.getStandardInformation().getAllocationSize();

          // this partis problematic as it throws an IOException
          // if not the fileSize amount of bytes can be read
          byte[] bytes = IOUtils.toByteArray(fileIn, fileInfo.getStandardInformation().getAllocationSize());
      }
   }
java smbj
1个回答
0
投票

为什么要在意长度呢? 就不能用吗

byte[] IOUtils.toByteArray(InputStream inputStream)

并让该实用程序读取直到到达文件末尾?

© www.soinside.com 2019 - 2024. All rights reserved.