简体   繁体   English

使用java.nio.Files更改Linux下的文件所有者组

[英]Change file owner group under Linux with java.nio.Files

I have a Linux server and I'm running an image resize job in Java for multiple websites on my server. 我有一台Linux服务器,我在Java上为我服务器上的多个网站运行图像大小调整作业。 The website files are owned by different OS users/groups. 网站文件归不同的OS用户/组所有。 Newly created thumbnails/previews are owned by the user running the resize job. 新创建的缩略图/预览由运行调整大小作业的用户拥有。 Now I was googleing around how to change the file owner of newly created previews/thumbnails in my resize program and came across this: 现在,我正在调整如何在我的调整大小程序中更改新创建的预览/缩略图的文件所有者,并发现了这个:

java.nio.file.Files.setOwner(Path path, UserPrincipal owner);

This would really solve my problem if it was Windows, but since a Linux file has a user and a group as owner I'm a bit in trouble. 如果它是Windows,这将真正解决我的问题,但由于Linux文件有一个用户和一个组作为所有者我有点麻烦。 Unfortunately given method seems to only change the user ownership of the file. 不幸的是,给定的方法似乎只改变了文件的用户所有权。 The group ownership remains with the group of the user running my Java resize job. 组所有权保留在运行Java调整大小作业的用户组中。

The websites are owned by different groups, so adding my resize job user to one group is no option. 这些网站由不同的群组拥有,因此将我的调整大小作业用户添加到一个群组是不可取的。 I also want to avoid system calls with ProcessBuilder and execute a chown on my files. 我还想避免使用ProcessBuilder进行系统调用并在我的文件上执行chown

I do need to point out that the created files (preview/thumbnail) can be accessed via the website and it is not mission critical to change the group ownership, but I wanted it to be as clean as possible. 我需要指出创建的文件(预览/缩略图)可以通过网站访问,更改组所有权并不是关键任务,但我希望它尽可能干净。

Any suggestions how I can change the group ownership of a file in Linux only using Java? 有关如何仅使用Java更改Linux中文件的组所有权的任何建议?

Thanks Jim Garrison for pointing me in the correct direction. 谢谢吉姆加里森指出我正确的方向。 Here the code, which finally solved the problem for me. 这里的代码,最终解决了我的问题。

Retrieve the group owner of a file 检索文件的组所有者

File originalFile = new File("original.jpg"); // just as an example
GroupPrincipal group = Files.readAttributes(originalFile.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group();

Set the group owner of a file 设置文件的组所有者

File targetFile = new File("target.jpg");
Files.getFileAttributeView(targetFile.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group);

I missed a complete solution, here it comes (combination of other answers and comments): 我错过了一个完整的解决方案,它来了(其他答案和评论的组合):

Path p = your file's Path;
String group = "GROUP_NAME";
UserPrincipalLookupService lookupService = FileSystems.getDefault()
            .getUserPrincipalLookupService();
GroupPrincipal group = lookupService.lookupPrincipalByGroupName(group);
Files.getFileAttributeView(p, PosixFileAttributeView.class,
            LinkOption.NOFOLLOW_LINKS).setGroup(group);

Be aware that only the owner of a file can change its group and only to a group he is a member of... 请注意,只有文件的所有者才能更改其组,并且只能更改为他所属的组...

Take a look at the package java.nio.file.attributes and class PosixFilePermissions . 看一下包java.nio.file.attributes和类PosixFilePermissions This is where you can manipulate group permissions. 您可以在此处理组权限。

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

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