linux中java创建文件所有者,使用java.nio.Files在Linux下更改文件所有者组

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镜像调整作业。网站文件由不同的OS用户/组拥有。新创建的缩略图/预览是由用户运行resize作业的。现在,我在google上搜索如何在resize程序中更改新创建的预览/缩略图的文件所有者,然后发现:

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 resize作业的用户组中。

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.

这些网站由不同的组拥有,所以不能将我的resize job用户添加到一个组中。我还希望避免使用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中文件的组所有权。

3 个解决方案

#1

22

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);

#2

13

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...

请注意,只有文件的所有者才能更改它的组,并且只对其所属的组进行更改。

#3

3

Take a look at the package java.nio.file.attributes and classPosixFilePermissions. This is where you can manipulate group permissions.

查看包java.nio.file。属性和classPosixFilePermissions。您可以在这里操作组权限。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值