- 步骤:
授予新权限的步骤: * 1) 调用FileSystem.getUserPrincipalLookupService()方法查看 * 2) 获得 ACL 视图 * 3) 使用 AclEntry.Builder 对象创建一个新的实体 * 4) 读取 ACL * 5) 插入新的实体 * 6) 使用 setAcl() 或 setAttribute() 重写 ACL
- 代码
Path path = Paths.get("E:/hsm.sql"); try { //1 lookup for the principal UserPrincipal user = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("icer"); //2 get the ACL view AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class); //3 create a new entry AclEntry entry = AclEntry.newBuilder().setType(AclEntryType.ALLOW) .setPrincipal(user).setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.APPEND_DATA).build(); //4 read acl List<AclEntry> acl = view.getAcl(); //5 Insert the new entry acl.add(0, entry); //6.1 rewrite acl // view.setAcl(acl); //6.2rewrite acl Files.setAttribute(path, "acl:acl", acl, LinkOption.NOFOLLOW_LINKS); } catch (Exception e) { e.printStackTrace(); }
授予文件新的权限---ACL
最新推荐文章于 2022-05-17 16:15:23 发布
这篇博客介绍了如何使用Java NIO2的ACL功能来授予文件新权限的步骤,包括查找用户主体、获取ACL视图、创建新权限条目、插入权限、最后重写ACL,以允许用户进行读取和追加数据操作。
摘要由CSDN通过智能技术生成