java中修改linux文件夹和文件的权限

/**
     * 修改linux图片权限
     * @param filePath
     * @throws IllegalStateException
     * @throws IOException
     */
    public static void storeFile(String filePath) throws IllegalStateException, IOException {
        File file = new File(filePath);
        //设置权限
        Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
        perms.add(PosixFilePermission.OWNER_READ);//设置所有者的读取权限
        perms.add(PosixFilePermission.OWNER_WRITE);//设置所有者的写权限
        perms.add(PosixFilePermission.OWNER_EXECUTE);//设置所有者的执行权限
        perms.add(PosixFilePermission.GROUP_READ);//设置组的读取权限
        perms.add(PosixFilePermission.GROUP_EXECUTE);//设置组的读取权限
        perms.add(PosixFilePermission.OTHERS_READ);//设置其他的读取权限
        perms.add(PosixFilePermission.OTHERS_EXECUTE);//设置其他的读取权限
        try {
            //设置文件和文件夹的权限
            Path pathParent = Paths.get(file.getParentFile().getAbsolutePath());
            Path pathDest = Paths.get(file.getAbsolutePath());
            Files.setPosixFilePermissions(pathParent, perms);//修改文件夹路径的权限
            Files.setPosixFilePermissions(pathDest, perms);//修改图片文件的权限
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_39706128/article/details/84582846