我需要使用 SMBJ 客户端将数据附加到现有文件。下面的代码附加数据,但最终文件已损坏,无法读取/打开文件。
Set<AccessMask> accessMask = new HashSet<>();
accessMask.add(AccessMask.GENERIC_READ);
accessMask.add(AccessMask.GENERIC_WRITE);
Set<FileAttributes> fileAttributes = new HashSet<>();
fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_NORMAL);
Set<SMB2CreateOptions> createOptions = new HashSet<>();
createOptions.add(SMB2CreateOptions.FILE_RANDOM_ACCESS);
File file = share.openFile("PATH", accessMask, fileAttributes, SMB2ShareAccess.ALL,
SMB2CreateDisposition.FILE_OPEN_IF, createOptions);
// Approach - 1
long fileOffset = 0;
byte[] buffer = new byte[1024*4];
int length = inputStream.read(buffer);
while(length != -1){
fileOffset = share.getFileInformation("PATH").getStandardInformation().getEndOfFile();
file.write(buffer, fileOffset, 0, lenght);
}
//Approach - 2
OutputStream oStream = f.getOutputStream();
oStream.write(fileContents.getBytes());
oStream.flush();
oStream.close();
但是从这两种方法来看,都无法正确附加数据。