请帮帮忙。我无法使用SMBJ创建和写入文件。我得到了一个错误:
com.hierynomus.mssmb2.SMBApiException: STATUS_OBJECT_NAME_NOT_FOUND (0xc0000034): Create failed for <file path>这是Windows错误还是SMBJ错误?我是否正确地使用了SMBJ?我不太理解Windows文件属性/选项。
String fileName ="EricTestFile.txt";
String fileContents = "Mary had a little lamb.";
SMBClient client = new SMBClient();
try (Connection connection = client.connect(serverName)) {
AuthenticationContext ac = new AuthenticationContext(username, password.toCharArray(), domain);
Session session = connection.authenticate(ac);
// Connect to Share
try (DiskShare share = (DiskShare) session.connectShare(sharename)) {
for (FileIdBothDirectoryInformation f : share.list(folderName, "*.*")) {
System.out.println("File : " + f.getFileName());
}
//share.openFile(path, accessMask, attributes, shareAccesses, createDisposition, createOptions)
Set<FileAttributes> fileAttributes = new HashSet<>();
fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_NORMAL);
Set<SMB2CreateOptions> createOptions = new HashSet<>();
createOptions.add(SMB2CreateOptions.FILE_RANDOM_ACCESS);
File f = share.openFile(folderName+"\\"+fileName, new HashSet(Arrays.asList(new AccessMask[]{AccessMask.GENERIC_ALL})), fileAttributes, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OVERWRITE, createOptions);
OutputStream oStream = f.getOutputStream();
oStream.write(fileContents.getBytes());
oStream.flush();
oStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}发布于 2019-01-15 14:52:01
如果您试图打开的文件还不存在,则需要使用不同的SMB2CreateDisposition。您现在使用的是FILE_OVERWRITE,它被记录为:
如果文件已经存在,则覆盖它;否则,失败操作。不得用于打印机对象。
您可能希望使用FILE_OVERWRITE_IF,它可以:
如果该文件已经存在,则覆盖它;否则,创建该文件。此值不应用于打印机对象。
https://stackoverflow.com/questions/54162825
复制相似问题