java 设置文件权限总是返回 FALSE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14052388/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:53:22  来源:igfitidea点击:

Setting file permissions returns FALSE always

javafilepermissionsiofile-permissions

提问by urir

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.

在 Ubuntu 上,所有 3 个调用都返回 false。在我的 Windows 上,只有第三次调用 setWritable 返回 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.

目标是创建文件/目录,以便用户(tomcat)和组能够读/写。
但是在 Ubuntu 上创建的文件没有该组的写入权限。

回答by urir

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

然后它会工作:)

回答by Bhavik Shah

from javadocs

来自 javadocs

setExecutable():Returns

setExecutable():返回

true if and only if the operation succeeded. The operation will fail if the user does not have permission to change the access permissions of this abstract pathname. If executable is false and the underlying file system does not implement an execute permission, then the operation will fail.

当且仅当操作成功时才​​为真。如果用户无权更改此抽象路径名的访问权限,则操作将失败。如果executable 为false 并且底层文件系统没有实现执行权限,则操作将失败。

Also,

还,

File(String pathname)Creates a new File instanceby converting the given pathname string into an abstract pathname. It creates a file instance. It does notcreate a new file.

File(String pathname)通过将给定的路径名​​字符串转换为抽象路径名来创建一个新的File 实例。它创建一个文件实例。它不会创建新文件。

To create a new file

创建新文件

File f;
  f=new File("myfile.txt");
  if(!f.exists()){
  f.createNewFile();
  System.out.println("New file \"myfile.txt\" has been created 
  to the current directory");
  }

回答by Vallabh Patade

Maybe you are not running this as a Super User in Linux. It can be the case that you(Logged in as) itself don't have rights to give file permissions.

也许您不是在 Linux 中以超级用户身份运行它。可能是您(以身份登录)本身无权授予文件权限。