/** * 給文件以及父文件夾賦予權限 * @param path String類型的路徑,要求是本地絕對路徑 */ public static void givePrevilege(String path) { if (!PubFun.isEmpty(path)) { 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 { if (path.contains(".pdf")) { //給生成的pdf文件賦予權限 Path pathDest = Paths.get(path); Files.setPosixFilePermissions(pathDest, perms); mLogger.info(path + " 文件賦權成功"); } else { //給pdf文件的父目錄賦予權限 Path pathDest = Paths.get(path); Files.setPosixFilePermissions(pathDest, perms); mLogger.info(path + " 文件夾賦權成功"); } //獲取路徑的文件對象,而後給父文件夾賦予權限 File file = new File(path); String parentPath = file.getParent(); givePrevilege(parentPath); } catch (IOException e) { mLogger.info(path + " 文件賦權失敗"); e.printStackTrace(); } } }