首页 > 编程语言 >java 改变liunx服务器文件权限和所属组所属用户

java 改变liunx服务器文件权限和所属组所属用户

时间:2022-10-26 15:58:36浏览次数:45  
标签:java lookupService liunx file false 所属 名称

private void setFilePermission(File file) {
        try {
            UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
            GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName("名称");
            UserPrincipal userPrincipal = lookupService.lookupPrincipalByName("名称");
            Path path = Paths.get(file.getPath());
            PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);
            view.setGroup(groupPrincipal);
            view.setOwner(userPrincipal);
            
            file.setExecutable(true, false);
            file.setReadable(true,false);
            file.setWritable(true, false);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

liunx手动更改指令

设置权限

chmod -R 777 目录名称

设置所属组

chgrp -R 所属组名称 目录名称

chown -R 所属用户名称 目录名称

 

如果只是单个文件操作,不用带-R,目录名改为文件名

标签:java,lookupService,liunx,file,false,所属,名称
From: https://www.cnblogs.com/nicopoiduang/p/16828675.html

相关文章