设置文件权限始终返回FALSE [英] Setting file permissions returns FALSE always

查看:618
本文介绍了设置文件权限始终返回FALSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

 文件dir = new File(path); 
布尔型rc1 = dir.setExecutable(true,false);
布尔型rc2 = dir.setReadable(true,false);
布尔型rc3 = dir.setWritable(true,false);
if(!rc1 ||!rc2 ||!rc3){
logger.warn(其中一个权限设置返回false:rc1 =+ rc1 +rc2 =+ rc2 +rc3 = + rc3 +[for dir'+ dir +']);





在Ubuntu上,所有3个调用都返回false。
在我的Windows上,只有第三次调用setWritable返回false。

目标是创建文件/目录,以便用户(tomcat)和组能够读/写。

但在Ubuntu上创建的文件没有权限写组。

我找到了解决方案,并会回答我自己的问题:

在文件或目录设置权限时,首先必须实际创建目录或写入文件,然后设置权限。
所以,我在启动时做的是错误的:

$ p $ 文件dir = new File(path);
布尔型rc1 = dir.setExecutable(true,false);

实际上需要:

 文件dir = new File(path); 
dir.mkdirs();
布尔型rc1 = dir.setExecutable(true,false);
布尔型rc2 = dir.setReadable(true,false);
布尔型rc3 = dir.setWritable(true,false);

 文件f =新文件(uploadedFileLocation); 
ImageIO.write(image,jpg,f);
布尔型rc1 = f.setExecutable(true,false);
boolean rc2 = f.setReadable(true,false);
布尔型rc3 = f.setWritable(true,false);

然后就可以了:)

The code:

File dir = new File(path);
boolean rc1 = dir.setExecutable(true, false);
boolean rc2 = dir.setReadable(true, false);
boolean rc3 = dir.setWritable(true, false);
if (!rc1 || !rc2 || !rc3){
    logger.warn("One of the permissions set returned false: rc1="+rc1+" rc2="+rc2+" rc3="+rc3 + " [for dir '"+dir+"']");
}

On Ubuntu all 3 calls return false. On my Windows only the 3rd call to setWritable returns false.

The target is to create the file/dir so the user (tomcat) and the group will be able to read/write.
BUT the file created on Ubuntu without permissions for the group to write.

解决方案

I found the solution and will answer my own question:
When setting permissions on file or directory, you first MUST actually create the directory or write the file and only then set the permissions.
So, what I was doing at start was wrong:

File dir = new File(path);
boolean rc1 = dir.setExecutable(true, false);

While actually need to:

File dir = new File(path);
dir.mkdirs();
boolean rc1 = dir.setExecutable(true, false);
boolean rc2 = dir.setReadable(true, false);
boolean rc3 = dir.setWritable(true, false);

or

    File f = new File(uploadedFileLocation);
    ImageIO.write(image, "jpg", f);
    boolean rc1 = f.setExecutable(true, false);
    boolean rc2 = f.setReadable(true, false);
    boolean rc3 = f.setWritable(true, false);

Then it will work :)

这篇关于设置文件权限始终返回FALSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆