从共享驱动器读取文件时出现 com.hierynomus.mssmb2.SMBApiException: STATUS_ACCESS_DENIED (0xc0000022) 异常
Getting com.hierynomus.mssmb2.SMBApiException: STATUS_ACCESS_DENIED (0xc0000022) exception while reading files from shared drive
我正在尝试使用 SMB Java 库从共享驱动器访问文件。
我正在尝试两种方式
a) SMB JCIFS
b) hierynomus
但是我得到异常的两种方式,下面是我得到的代码和异常
JCIFS
try {
String path = "smb://" + remote_machine_name + "/" + sharedFolder+"/";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", remote_user_id, remote_user_password);
SmbFile smbFile = new SmbFile(path, auth);
System.out.println("Connected");
SmbFile[] smbFileList = smbFile.listFiles();
for (SmbFile file : smbFileList) {
System.out.println(file.getName());
}
System.out.println("Done");
} catch (SmbException se) {
se.printStackTrace();
}
异常:
jcifs.smb1.smb1.SmbException: Failed to connect: <hostname>/<ip address>
jcifs.smb1.util.transport.TransportException
java.net.SocketException: Connection reset
hierynomus smbj
SmbConfig cfg = SmbConfig.builder().withSecurityProvider(new BCSecurityProvider()).build();
SMBClient client = new SMBClient(cfg);
Connection connection;
try {
connection = client.connect(remote_machine_name);
Session session = connection
.authenticate(new AuthenticationContext(remote_user_id, remote_user_password.toCharArray(), null));
DiskShare share = (DiskShare) session.connectShare(sharedFolder);
Set<FileAttributes> fileAttributes = new HashSet<>();
fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_DIRECTORY);
Set<SMB2ShareAccess> shareAccesses = new HashSet<SMB2ShareAccess>();
shareAccesses.add(SMB2ShareAccess.FILE_SHARE_READ);
for (FileIdBothDirectoryInformation f : share.openDirectory(
"folder1/folder11/folder111",
EnumSet.of(AccessMask.FILE_READ_DATA), fileAttributes, shareAccesses,
SMB2CreateDisposition.FILE_OPEN, null)) {
System.out.println(f.getFileName());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
异常(完整的堆栈跟踪):
[main] INFO com.hierynomus.smbj.connection.Connection - Successfully connected to: **<hostname>**
[main] INFO com.hierynomus.smbj.connection.SMBSessionBuilder - Successfully authenticated karthik on **<hostname>**, session is -12346324324
[main] INFO com.hierynomus.smbj.session.Session - Connecting to **\hostname\proj** on session -12346324324
Exception in thread "main" com.hierynomus.mssmb2.SMBApiException: STATUS_ACCESS_DENIED (0xc0000022): Create failed for \hostname\proj\Folder1\Folder11\Folder111
at com.hierynomus.smbj.share.Share.receive(Share.java:397)
at com.hierynomus.smbj.share.Share.sendReceive(Share.java:377)
at com.hierynomus.smbj.share.Share.createFile(Share.java:159)
at com.hierynomus.smbj.share.DiskShare.createFileAndResolve(DiskShare.java:75)
at com.hierynomus.smbj.share.DiskShare.access0(DiskShare.java:55)
at com.hierynomus.smbj.share.DiskShare.apply(DiskShare.java:109)
我手动将网络驱动器映射到我的系统并验证我有管理员权限,但由于某些原因我收到上述错误。
谁能帮我解决一下
我从我的另一个答案中找到了 SMB JCIFS 方式的可能解决方案!
我认为问题是您的系统中没有启用 SMB2(这是 windows 10 所必需的)并且您需要创建一个 CIFSContext
对象来在您的系统之间建立正确的连接本地电脑和共享文件夹。
我正在尝试使用 SMB Java 库从共享驱动器访问文件。
我正在尝试两种方式
a) SMB JCIFS b) hierynomus
但是我得到异常的两种方式,下面是我得到的代码和异常
JCIFS
try {
String path = "smb://" + remote_machine_name + "/" + sharedFolder+"/";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", remote_user_id, remote_user_password);
SmbFile smbFile = new SmbFile(path, auth);
System.out.println("Connected");
SmbFile[] smbFileList = smbFile.listFiles();
for (SmbFile file : smbFileList) {
System.out.println(file.getName());
}
System.out.println("Done");
} catch (SmbException se) {
se.printStackTrace();
}
异常:
jcifs.smb1.smb1.SmbException: Failed to connect: <hostname>/<ip address>
jcifs.smb1.util.transport.TransportException
java.net.SocketException: Connection reset
hierynomus smbj
SmbConfig cfg = SmbConfig.builder().withSecurityProvider(new BCSecurityProvider()).build();
SMBClient client = new SMBClient(cfg);
Connection connection;
try {
connection = client.connect(remote_machine_name);
Session session = connection
.authenticate(new AuthenticationContext(remote_user_id, remote_user_password.toCharArray(), null));
DiskShare share = (DiskShare) session.connectShare(sharedFolder);
Set<FileAttributes> fileAttributes = new HashSet<>();
fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_DIRECTORY);
Set<SMB2ShareAccess> shareAccesses = new HashSet<SMB2ShareAccess>();
shareAccesses.add(SMB2ShareAccess.FILE_SHARE_READ);
for (FileIdBothDirectoryInformation f : share.openDirectory(
"folder1/folder11/folder111",
EnumSet.of(AccessMask.FILE_READ_DATA), fileAttributes, shareAccesses,
SMB2CreateDisposition.FILE_OPEN, null)) {
System.out.println(f.getFileName());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
异常(完整的堆栈跟踪):
[main] INFO com.hierynomus.smbj.connection.Connection - Successfully connected to: **<hostname>**
[main] INFO com.hierynomus.smbj.connection.SMBSessionBuilder - Successfully authenticated karthik on **<hostname>**, session is -12346324324
[main] INFO com.hierynomus.smbj.session.Session - Connecting to **\hostname\proj** on session -12346324324
Exception in thread "main" com.hierynomus.mssmb2.SMBApiException: STATUS_ACCESS_DENIED (0xc0000022): Create failed for \hostname\proj\Folder1\Folder11\Folder111
at com.hierynomus.smbj.share.Share.receive(Share.java:397)
at com.hierynomus.smbj.share.Share.sendReceive(Share.java:377)
at com.hierynomus.smbj.share.Share.createFile(Share.java:159)
at com.hierynomus.smbj.share.DiskShare.createFileAndResolve(DiskShare.java:75)
at com.hierynomus.smbj.share.DiskShare.access0(DiskShare.java:55)
at com.hierynomus.smbj.share.DiskShare.apply(DiskShare.java:109)
我手动将网络驱动器映射到我的系统并验证我有管理员权限,但由于某些原因我收到上述错误。
谁能帮我解决一下
我从我的另一个答案中找到了 SMB JCIFS 方式的可能解决方案!
我认为问题是您的系统中没有启用 SMB2(这是 windows 10 所必需的)并且您需要创建一个 CIFSContext
对象来在您的系统之间建立正确的连接本地电脑和共享文件夹。