Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取Java中的SMB共享文件名和权限_Java_Python_Smb - Fatal编程技术网

获取Java中的SMB共享文件名和权限

获取Java中的SMB共享文件名和权限,java,python,smb,Java,Python,Smb,我想连接到SMB服务器并浏览其文件,并为给定路径检索具有名称和权限的文件和文件夹列表 我需要支持所有SMB方言,并且能够从我的代码中做到这一点 代码大致如下所示: smbClient.connect(serverInfo); info = smbClient.getShare(shareName); for(File file : info.getFiles) { List<permission> permissions = file.getPermissions();

我想连接到SMB服务器并浏览其文件,并为给定路径检索具有名称和权限的文件和文件夹列表

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

代码大致如下所示:

smbClient.connect(serverInfo);
info = smbClient.getShare(shareName);
for(File file : info.getFiles) {
    List<permission> permissions = file.getPermissions();
    //do something
}
smbClient.connect(serverInfo);
info=smbClient.getShare(shareName);
for(文件:info.getFiles){
List permissions=file.getPermissions();
//做点什么
}
我尝试了一些选项,如、,但它们似乎都不能满足我的上述要求


使用Java、Python或我可以从Java代码调用的任何linux CLI,是否有任何方法可以实现上述目标?

如果您运行的是Windows,并且运行该程序的用户可以访问共享,那么您可以使用Java.nio来实现这一点。nio允许您访问SMB共享

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

然后可以使用
acltribute.getAcls()我认为没有任何开源Java库支持您所需的一切

有一个名为“可视化系统”的非开源库

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

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


我们也遇到了同样的问题,并发现使用java最好地满足了我们的需求(尽管我们只在v1和v2上测试了它,但最新版本也支持v3)。 您的代码如下所示:

Configuration config = new PropertyConfiguration(new Properties());
CIFSContext context = new BaseContext(config);
context = context.withCredentials(new NtlmPasswordAuthentication(null, domain, userName, password));
String share = "smb://HOSTNAME/SHARENAME/";
try (SmbFile share = new SmbFile(url, context)) {
    for (SmbFile file : share.listFiles()) {
        ACE[] groups = file.getSecurity();
        // Do something..
    }
}
其中ACE是一个访问控制条目,它是一个控制或监视对对象的访问(简称权限)的元素


请注意,最新版本可能尚未在Maven或Gradle上发布,因此您必须克隆repo并自己构建它。

我想它可以帮助您改进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:

SingletonContext context = SingletonContext.getInstance();
CIFSContext testCtx = context.withCredentials(
    new NtlmPasswordAuthentication(
        context, domain, fileSystemInfo.getUsername(), fileSystemInfo.getPassword()
    )
);
SmbFile smbFile = new SmbFile(fullPath.replace('\', '/'), testCtx);
if (!smbFile.exists())
{
    smbFile.mkdirs();
}
smbFile.close();

根据需要读取共享文件的属性,如果只需要将其用作客户端,pysmb可能是一个很好的选择SharedFile类应该为您提供找到的文件属性:谢谢@BoboDarph,但是pysmb只支持方言1和2,并且我提到了支持所有SMB方言的要求。SMB3和3.1.1方言在这些API上没有改进,所以实际上,SMB2支持就足够了。SMB2是SMB1的重写,但SMB2.002、SMB2.1、SMB3.0和SMB3.1.1都是SMB2上的方言(有特定的改进),但都使用相同的数据包。所以smbj应该只为你工作。除非您连接的服务器已明确禁用SMB2或SMB2.1作为方言。谢谢@lech_uk。运行程序的用户无权访问此文件,但我手头有访问此文件的用户凭据。据我所知,无法通过编程方式使用给定的用户登录,而且根据JavaDoc AclFileAttributeView,它只支持NFS而不支持SMB。谢谢@Eliad我熟悉jNQ,但如上所述,我正在寻找免费的解决方案。谢谢,这帮助我解决了访问SMB文件的问题:)
SingletonContext context = SingletonContext.getInstance();
CIFSContext testCtx = context.withCredentials(
    new NtlmPasswordAuthentication(
        context, domain, fileSystemInfo.getUsername(), fileSystemInfo.getPassword()
    )
);
SmbFile smbFile = new SmbFile(fullPath.replace('\', '/'), testCtx);
if (!smbFile.exists())
{
    smbFile.mkdirs();
}
smbFile.close();