简体   繁体   English

如何在Java7中为特定用户设置文件访问属性

[英]How to set File Access attribute for a particular user in java7

i am using java7 File api. 我正在使用java7 File api。 for setting owner of a file i searched and able to change owner attribute. 用于设置我搜索的文件的所有者,并能够更改所有者属性。 my code is 我的代码是

public static void main(String[] args){

    Path zip=Paths.get("/home/ritesh/hello");
    try{ 
        FileOwnerAttributeView view 
             = Files.getFileAttributeView(zip,FileOwnerAttributeView.class);

        UserPrincipalLookupService lookupService
             =FileSystems.getDefaullt().getUserPrincipalLookupService();

        UserPrincipal owner=null;

        try{ owner =lookupService.lookupPrincipalByName("rashmi");}
        catch(UserPrincipalNotFoundException e){System.out.println("User not found");}

        view.setOwner(owner);

    } catch (IOException e){
        e.printStackTrace();}
    }

from this code i can set the owner of the file .but my task is to give a user(rashmi) Read access to a file and one more user to give Read/Write access.how to give particular access to a user please give some code or link so that i can accomplish my task. 从此代码中,我可以设置文件的所有者。但是我的任务是为用户提供(rashmi)对文件的读取访问权限,为另一个用户提供对文件的读/写访问权限。如何为用户提供特定的访问权限,请给一些代码或链接,以便我可以完成任务。

This is taken from the Java 7 Javadoc for AclFileAttributeView (with a little reformatting): 这取自Java 7 Javadoc的AclFileAttributeView (带有一些重新格式化):

 // lookup "joe"
 UserPrincipal joe = file.getFileSystem().getUserPrincipalLookupService()
     .lookupPrincipalByName("joe");

 // get view
 AclFileAttributeView view = Files.getFileAttributeView(file,
                                       AclFileAttributeView.class);

 // create ACE to give "joe" read access
 AclEntry entry = AclEntry.newBuilder()
     .setType(AclEntryType.ALLOW)
     .setPrincipal(joe)
     .setPermissions(AclEntryPermission.READ_DATA,
                     AclEntryPermission.READ_ATTRIBUTES)
     .build();

 // read ACL, insert ACE, re-write ACL
 List<AclEntry> acl = view.getAcl();
 acl.add(0, entry);   // insert before any DENY entries
 view.setAcl(acl);

You need to use the right AclEntry code. 您需要使用正确的AclEntry代码。 See the AclFileAttributeView Javadoc . 请参阅AclFileAttributeView Javadoc

you have to understand permission policy of the OS, you cant specify different permissions for different users, you have to do it other way. 您必须了解操作系统的权限策略,不能为不同的用户指定不同的权限,而必须采用其他方式。

file permissions is defined with 3 Octal numbers (base 8), which is 3 groups of 3 binary numbers, each 3 bits stands for Read,Write,Execute the first group is the Owner , second is Group , third is Other 文件权限由3个八进制数字(以8为底)定义,这是3组3个二进制数字,每3位代表Read,Write,Execute第一个组是Owner ,第二个是Group ,第三个是Other

so, if you want to give different permissions you can set the Group attribute to have access for reading and writing, and the Other just to reading, afterwards, you have to add the user you want to be able to Read and Write to this group. 因此,如果您要赋予不同的权限,则可以将Group属性设置为具有读写访问权限,然后将Other设置为具有读取权限,然后,您必须将希望能够读写的用户添加到该组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM