在J中获取SMB共享文件的名称和权限

2024-05-15 22:22:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我想连接到一个SMB服务器并浏览它的文件,对于一个给定的路径,我可以检索文件和文件夹的列表,以及名称和权限。在

我需要支持所有的SMB方言,并能够从我的代码做到这一点。在

代码大致如下:

smbClient.connect(serverInfo);
info = smbClient.getShare(shareName);
for(File file : info.getFiles) {
    List<permission> permissions = file.getPermissions();
    //do something
}

我尝试过一些选项,比如smbjimpacketnmapsamba,但它们似乎都不能满足我上面的要求。在

有没有任何方法可以实现上述目的,使用Java、Python或任何可以从Java代码调用的linuxcli?在


Tags: 文件代码路径info服务器文件夹名称权限
3条回答

如果您正在运行Windows,并且运行该程序的用户有权访问该共享,则可以使用java.nio软件. Java.nio软件允许您访问SMB共享。在

Path path = Paths.get(<SharedFile>);
AclFileAttributeView aclAttribute = Files.getFileAttributeView(path, AclFileAttributeView.class);

然后可以使用aclAttribute.getAcls();获取用户及其权限。在

我想它可以帮助你提高jcifs ng。在

**// Option 1 - SMB2 and SMB3:**
Properties prop = new Properties();
prop.put( "jcifs.smb.client.enableSMB2", "true");
prop.put( "jcifs.smb.client.disableSMB1", "false");
prop.put( "jcifs.traceResources", "true" );
Configuration config = new PropertyConfiguration(prop);
CIFSContext baseContext = new BaseContext(config);
CIFSContext contextWithCred = baseContext.withCredentials(new NtlmPasswordAuthentication(baseContext, domain, fileSystemInfo.getUsername(), fileSystemInfo.getPassword()));
SmbFile share = new SmbFile(fullPath.replace('\', '/'), contextWithCred);
if (!share.exists())
{
    share.mkdirs();
}
share.close();

//选项2-SMB1和CIFS:

^{pr2}$

我不认为有任何开源Java库可以支持您所需要的一切。在

有一个叫做jNQ的非开源库被称为“可视化系统”

此库支持所有SMB方言(SMB1到SMB3.1.1)

在链接中有一个用于浏览的代码示例(您可以获得列表中每个文件的安全描述符):

PasswordCredentials cr = new PasswordCredentials("userName", "password", "domain");
Mount mt = new Mount("IpAddress","ShareName", cr);
Directory dir = new Directory(mt, "dir1");
Directory.Entry entry;
System.out.println(DIR + " scan:");
do {
    entry = dir.next();
    if (null != entry)
        System.out.println(entry.name + " : size = " + entry.info.eof);
} while (entry != null);

相关问题 更多 >