详解如何用java增加文件的用户访问权限

这里代码实现的是为一个图片文件增加所有用户的完全控制权限
 

 

实现代码

//修改访问权限
                    Path path= Paths.get(file.getPath());
                    AclFileAttributeView aclView= Files.getFileAttributeView(path,AclFileAttributeView.class);
                    if (aclView == null) {
                        System.out.format("ACL view  is not  supported.%n");
                        return;
                    }
                    try {
                        UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();

                        GroupPrincipal group = lookupService.lookupPrincipalByGroupName("Everyone");

                        AclEntry.Builder builder = AclEntry.newBuilder();
                        builder.setPrincipal(group);
                        builder.setType(AclEntryType.ALLOW);

                        //搜索之前所有用户权限
                        List<AclEntry> aclEntries = aclView.getAcl();
                        Set<AclEntryPermission> permissions = aclEntries.get(0).permissions();

                        builder.setPermissions(permissions);
                        AclEntry newEntry = builder.build();

                        //把新的用户权限加入列表
                        aclEntries.add(newEntry);

                        aclView.setAcl(aclEntries);

                        System.out.println("用户权限列表:");
                        for (AclEntry entry : aclEntries) {
                            System.out.format("Principal: %s%n", entry.principal());
                            System.out.format("Type: %s%n", entry.type());
                            System.out.format("Permissions are:%n");

                            permissions = entry.permissions();
                            for (AclEntryPermission p : permissions) {
                                System.out.format("%s %n", p);
                            }
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

总体的代码参考链接:java修改文件所有者及其权限
 
 

逐步详解代码

1. 获取文件视图
Path path= Paths.get(file.getPath());

Path和Files类封装了在用户机器上处理文件系统所需的所有功能
Path和Files是在Java SE 7 中新添加进来的类,在传统java.io中, 文件和目录都被抽象成File对象, 即 File file = new File(".");
NIO.中则引入接口Path代表与平台无关的路径,文件和目录都用Path对象表示
通过路径工具类Paths返回一个路径对象Path
Paths.get()是一个创建Path的工厂方法,需要传入一个绝对路径作为参数。
上边我们传入的是Windows系统的文件路径格式,file.getPath()的内容是:
"C:\\Users\\ccy\\Pictures\\数据库课设图片\\foodMenu\\upload"
参考链接:Java Path与Files
     Java NIO总结(三):Path和Files工具类

 

                    AclFileAttributeView aclView= Files.getFileAttributeView(path,AclFileAttributeView.class);
                    if (aclView == null) {
                        System.out.format("ACL view  is not  supported.%n");
                        return;
                    }

AclFileAttributeView:
一个文件属性视图,支持读取或更新文件的访问控制列表(ACL)或文件所有者属性。
ACL用于指定文件系统对象的访问权限。
ACL是access-control-entries的有序列表,每个列表指定UserPrincipal以及该用户主体的访问级别。
此文件属性视图定义getAcl和setAcl方法,以根据RFC 3530: Network File System (NFS) version 4 Protocol中指定的ACL模型读取和写入ACL。
参考链接:java11教程–接口AclFileAttributeView用法
 
getFileAttributeView():
返回给定类型的文件属性视图。
public static <V extends FileAttributeView> V getFileAttributeView​(Path path, 类<V> type, LinkOption... options)
文件属性视图提供一组文件属性的只读或可更新视图。
此方法适用于文件属性视图定义用于读取或更新文件属性的类型安全方法的情况。
type参数是所需属性视图的类型,如果支持,该方法将返回该类型的实例。
BasicFileAttributeView类型支持访问文件的基本属性。
调用此方法以选择该类型的文件属性视图将始终返回该类的实例。
参考链接:Files - Java 11中文版 - API参考文档
 
AclFileAttributeView.class:
表示获取AclFileAttributeView的class对象,类型类指的是代表一个类型的类
 

2. 通过文件视图获取文件权限列表
                        //aclView.getAcl():获取视图的访问权限列表
                        List<AclEntry> aclEntries = aclView.getAcl();
                        //获取第一个文件系统对象(在我这里获取到的是system组)的访问权限中的permissions
                        Set<AclEntryPermission> permissions = aclEntries.get(0).permissions();

 

3. 在权限列表中增加对应权限条目
                         UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
                         GroupPrincipal group = lookupService.lookupPrincipalByGroupName("Everyone");

FileSystems:
文件系统的工厂方法。 此类定义getDefault方法以获取默认文件系统和工厂方法以构造其他类型的文件系统
getUserPrincipalLookupService方法返回UserPrincipalLookupService以按名称查找用户或组。
参考链接:FileSystems - Java 11中文版 - API参考文档
 
GroupPrincipal:
GroupPrincipal表示组标识 。另:UserPrincipal表示可用于确定对文件系统中的对象的访问权限的标识。
此处可以得到\Everyone (Well-known group)组,表示所有用户都可以访问

                        //AclEntry是acl的一个条目,包含type,Principal,permissions,flags四个部分,通过AclEntry.Builder创建
                        //一个Builder对象是通过调用AclEntry类的newBuilder()方法获得的
                        AclEntry.Builder builder = AclEntry.newBuilder();
                        
                        builder.setPrincipal(group);
                        /*AclEntryType
                        A typesafe enumeration of the access control entry types.
                        ALLOW:Explicitly grants access to a file or directory.
                        (来自官方文档)
                         */
                        builder.setType(AclEntryType.ALLOW);
                        builder.setPermissions(permissions);

                        //创建AclEntry条目
                        AclEntry newEntry = builder.build();

                        //把新的用户权限加入acl权限列表
                        aclEntries.add(newEntry);

                        //将该权限列表应用于视图中
                        aclView.setAcl(aclEntries);

打印结果

结果1
结果2
图1是原本就存在的system权限列表,图2是新增的everyone权限列表

写在最后

我也是为了做项目需求才刚开始接触、学习用java修改文件的用户访问权限。刚开始看这些内容真是一头雾水,各种解析也都是一点点自己查出来的。学习过程中在网上找了很多资料,竟然没有一篇文章提供一个系统的完整的代码解析。于是我自己查完这些资料后就汇总了一下,发表了这篇文章。
如果里面有不对的地方,也希望得到指正,大家一起学习进步~!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值