我正在尝试使用 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());
}
}