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

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,目录名改为文件名

posted @ 2022-10-26 15:45  生命不息bug不止  阅读(273)  评论(0编辑  收藏  举报