使用 java.nio.Files 在 Linux 下更改文件所有者组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13241967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change file owner group under Linux with java.nio.Files
提问by Peter Ilfrich
I have a Linux server and I'm running an image resize job in Java for multiple websites on my server. The website files are owned by different OS users/groups. 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:
我有一台 Linux 服务器,我正在 Java 中为服务器上的多个网站运行图像大小调整作业。网站文件归不同的操作系统用户/组所有。新创建的缩略图/预览归运行调整大小作业的用户所有。现在我正在搜索如何在我的调整大小程序中更改新创建的预览/缩略图的文件所有者并遇到了这个:
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. 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.
如果是 Windows,这真的可以解决我的问题,但是由于 Linux 文件有一个用户和一个组作为所有者,所以我有点麻烦。不幸的是,给定的方法似乎只会更改文件的用户所有权。组所有权仍属于运行我的 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
在我的文件上执行 a 。
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 中更改文件的组所有权?
采纳答案by Peter Ilfrich
Thanks Jim Garrison for pointing me in the correct direction. Here the code, which finally solved the problem for me.
感谢 Jim Garrison 为我指明了正确的方向。这是代码,最终为我解决了问题。
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);
回答by Jim Garrison
Take a look at the package java.nio.file.attributes
and classPosixFilePermissions
. This is where you can manipulate group permissions.
看看 packagejava.nio.file.attributes
和 class PosixFilePermissions
。您可以在此处操作组权限。
回答by adler
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...
请注意,只有文件的所有者才能更改其组,并且只能更改为他所属的组...