欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 如何为文件及文件夹添加权限

程序员文章站 2022-03-25 13:37:32
目录2.acl文件权限3.posix文件权限java 为文件及文件夹添加权限/** * 增加权限,使路径可上传文件 */public static void addchmod777(string fi...

java 为文件及文件夹添加权限

/**
 * 增加权限,使路径可上传文件
 */
public static void addchmod777(string filepath) throws ioexception {
	if (!system.getproperty("os.name").startswith("win")) {
		string cmdgrant = "chmod 777 " + filepath;
		baselogmethod.loginfo(tag, "file augmentation after moving:" + cmdgrant);
		runtime.getruntime().exec(cmdgrant);
	}
}
/**
 * 增加权限,使路径及子路径都有权限
 */
public static void addrchmod777(string filepath) throws ioexception {
	if (!system.getproperty("os.name").startswith("win")) {
		string cmdgrant = "chmod -r 777 " + filepath;
		baselogmethod.loginfo(tag, ".addrchmod777: file augmentation after moving:" + cmdgrant);
		runtime.getruntime().exec(cmdgrant);
	}
}

java 修改文件所有者及其权限

1.设置所有者

管理文件所有者

files.getowner()和files.setowner()方法

要使用userprincipal来管理文件的所有者

(1)更改文件的所有者

java 如何为文件及文件夹添加权限

import java.io.ioexception;
import java.nio.file.*;
import java.nio.file.attribute.fileownerattributeview;
import java.nio.file.attribute.userprincipal;
import java.nio.file.attribute.userprincipallookupservice;
public class main {
    public static void main(string[] args) {
        path path = paths.get("/www/test1.txt");
        fileownerattributeview foav = files.getfileattributeview(path,
                fileownerattributeview.class);
        try {
            userprincipal owner = foav.getowner();
            system.out.format("original owner  of  %s  is %s%n", path,
                    owner.getname());
            filesystem fs = filesystems.getdefault();
            userprincipallookupservice upls = fs.getuserprincipallookupservice();
            userprincipal newowner = upls.lookupprincipalbyname("abc");
            foav.setowner(newowner);
            userprincipal changedowner = foav.getowner();
            system.out.format("new owner  of  %s  is %s%n", path,
                    changedowner.getname());
        }catch (ioexception e){
            e.printstacktrace();
        }
    }
}

输出

java 如何为文件及文件夹添加权限

查看文件详细信息

java 如何为文件及文件夹添加权限

2.acl文件权限

windows上支持acl类型文件属性

使用aclfileattributeview的

  • getacl()方法获取文件的aclentry列表
  • setacl()方法设置文件的aclentry列表

(1)读取文件e:/test1.txt的acl条目

import java.io.ioexception;
import java.nio.file.files;
import java.nio.file.path;
import java.nio.file.paths;
import java.nio.file.attribute.aclentry;
import java.nio.file.attribute.aclentrypermission;
import java.nio.file.attribute.aclfileattributeview;
import java.util.list;
import java.util.set;
public class main {
    public static void main(string[] args) {       
        path path = paths.get("e:/test1.txt");
        aclfileattributeview aclview = files.getfileattributeview(path,
                aclfileattributeview.class);
        if (aclview == null) {
            system.out.format("acl view  is not  supported.%n");
            return;
        }
        try {
            list<aclentry> aclentries = aclview.getacl();
            for (aclentry entry : aclentries) {
                system.out.format("principal: %s%n", entry.principal());
                system.out.format("type: %s%n", entry.type());
                system.out.format("permissions are:%n");
                set<aclentrypermission> permissions = entry.permissions();
                for (aclentrypermission p : permissions) {
                    system.out.format("%s %n", p);
                }
            }
        } catch (ioexception e) {
            e.printstacktrace();
        }
    }
}

输出结果为

principal: builtin\administrators (alias)
type: allow
permissions are:
write_data
write_owner
append_data
synchronize
write_attributes
execute
read_data
delete_child
read_attributes
write_named_attrs
write_acl
delete
read_acl
read_named_attrs
principal: nt authority\system (well-known group)
type: allow
permissions are:
write_data
write_owner
append_data
synchronize
write_attributes
execute
read_data
delete_child
read_attributes
write_named_attrs
write_acl
delete
read_acl
read_named_attrs
principal: nt authority\authenticated users (well-known group)
type: allow
permissions are:
write_data
read_attributes
append_data
write_named_attrs
synchronize
write_attributes
execute
delete
read_data
read_acl
read_named_attrs
principal: builtin\users (alias)
type: allow
permissions are:
read_attributes
synchronize
execute
read_data
read_acl
read_named_attrs

(2)为指定用户添加新的acl条目

e:/test1.txt为用户abc添加data_read和data_ write权限

import java.io.ioexception;
import java.nio.file.filesystems;
import java.nio.file.files;
import java.nio.file.path;
import java.nio.file.paths;
import java.nio.file.attribute.*;
import java.util.enumset;
import java.util.list;
import java.util.set;
import static java.nio.file.attribute.aclentrypermission.read_data;
import static java.nio.file.attribute.aclentrypermission.write_data;
public class main {
    public static void main(string[] args) {
        
        path path = paths.get("e:/test1.txt");
        aclfileattributeview aclview = files.getfileattributeview(path,
                aclfileattributeview.class);
        if (aclview == null) {
            system.out.format("acl view  is not  supported.%n");
            return;
        }
        try {
            userprincipal briceuser = filesystems.getdefault()
                    .getuserprincipallookupservice().lookupprincipalbyname("abc");
            set<aclentrypermission> permissions = enumset.of(read_data, write_data);
            aclentry.builder builder = aclentry.newbuilder();
            builder.setprincipal(briceuser);
            builder.settype(aclentrytype.allow);
            builder.setpermissions(permissions);
            aclentry newentry = builder.build();
            list<aclentry> aclentries = aclview.getacl();
            aclentries.add(newentry);
            aclview.setacl(aclentries);
        }catch (ioexception e){
            e.printstacktrace();
        }
    }
}

输出结果比刚才多了

principal: hkgi-pc\abc (user)
type: allow
permissions are:
write_data
read_data

3.posix文件权限

unix支持posix标准文件属性

posixfilepermission枚举类型定义九个常量,每个权限组件一个。

九个常数命名为x_y,其中x是owner,group和others,y是read,write和execute。

  • posixfilepermissions的tostring()方法将一组posixfilepermission枚举常量转换为rwxrwxrwx形式的字符串
  • posixfileattributeview的setpermissions()方法用来设置权限

(1)输出/www/test1.txt的权限

import java.io.ioexception;
import java.nio.file.files;
import java.nio.file.path;
import java.nio.file.paths;
import java.nio.file.attribute.*;
import java.util.set;
public class main {
    public static void main(string[] args) {
        path path = paths.get("/www/test1.txt");
        posixfileattributeview posixview = files.getfileattributeview(path,
                posixfileattributeview.class);
        try{
            posixfileattributes attribs = posixview.readattributes();
            set<posixfilepermission> permissions = attribs.permissions();
            // convert the file permissions into the rwxrwxrwx string form
            string rwxformpermissions = posixfilepermissions.tostring(permissions);
            // print the permissions
            system.out.println(rwxformpermissions);
        }catch (ioexception e){
            e.printstacktrace();
        }
    }
}

输出结果

rw-r--r--

(2)读取和更新名为test的文件权限

import java.io.ioexception;
import java.nio.file.files;
import java.nio.file.path;
import java.nio.file.paths;
import java.nio.file.attribute.*;
import java.util.enumset;
import java.util.set;
import static java.nio.file.attribute.posixfilepermission.*;
public class main {
    public static void main(string[] args) {
        path path = paths.get("/www/test1.txt");
        posixfileattributeview posixview = files.getfileattributeview(path,
                posixfileattributeview.class);
        if (posixview == null) {
            system.out.format("posix attribute view  is not  supported%n.");
            return;
        }
        system.out.println("old:");
        readpermissions(posixview);
        updatepermissions(posixview);
        system.out.println("new:");
        readpermissions(posixview);
    }
    public static void readpermissions(posixfileattributeview posixview) {
        try{
            posixfileattributes attribs;
            attribs = posixview.readattributes();
            set<posixfilepermission> permissions = attribs.permissions();
            // convert the set of posix file permissions into rwxrwxrwx form
            string rwxformpermissions = posixfilepermissions.tostring(permissions);
            system.out.println(rwxformpermissions);
        }catch (ioexception e){
            e.printstacktrace();
        }
    }
    public static void updatepermissions(posixfileattributeview posixview) {
        try {
            set<posixfilepermission> permissions = enumset.of(owner_read, owner_write,owner_execute,
                    group_read,group_write);
            posixview.setpermissions(permissions);
            system.out.println("permissions set successfully.");
        }catch (ioexception e){
            e.printstacktrace();
        }
    }
}

输出结果

old:
rw-r-----
permissions set successfully.
new:
rwxrw----

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。