java.io.IOException:网络文件夹上的权限被拒绝

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

java.io.IOException: Permission denied on network folder

javaionetworkstream

提问by

i'm having the the post's title error when trying to write a file on a window folder , mounted on unix system. I've developed a web service which runs inside a Tomcat 6 on a linux os and need to write on a windows network folder. System administrators have mounted it on the Linux sever and have no problem to create and modify a file on it. When i try to execute the posted code i get the following exception :

尝试在安装在 unix 系统上的窗口文件夹上写入文件时,我遇到了帖子的标题错误。我开发了一个 web 服务,它在 linux os 上的 Tomcat 6 内运行,需要在 windows 网络文件夹上写入。系统管理员已经把它挂载在Linux服务器上,在上面创建和修改文件没有问题。当我尝试执行发布的代码时,出现以下异常:

Permission denied java.io.IOException: Permission denied at java.io.UnixFileSystem.createFileExclusively(Native Method) at java.io.File.createNewFile(File.java:850)

权限被拒绝 java.io.IOException:权限被拒绝在 java.io.UnixFileSystem.createFileExclusively(Native Method) at java.io.File.createNewFile(File.java:850)

The weird thing is that it seems to be related to the File.createNewFile method on a network folder , in fact the service can write on local file system without problems, both on debug (the pc i use to develop the service) and a tomcat folder system administrators have provided me on the linux server. The file gets created but is empty and the log entry following the create method doesn't get printed. Moreover if i use a plain outputstream to create and write the file i've no problems.

奇怪的是,它似乎与网络文件夹上的 File.createNewFile 方法有关,实际上该服务可以在本地文件系统上写入而没有问题,无论是在调试(我用来开发服务的电脑)还是一个 tomcat文件夹系统管理员在linux服务器上为我提供了。该文件已创建但为空,并且不会打印 create 方法后面的日志条目。此外,如果我使用普通输出流来创建和写入文件,我没有问题。

I cannot find any explanation about the exception on the web. Since i'm not very experienced with java , i'd like to understand why i'm getting this error. Am i using it in the wrong way ? Is it a bug of the library ? Do i miss to pass some parameter ? As stated , i've solved the problem using a plain outputstream, this is a question to improve my understanding of java.

我在网上找不到任何关于异常的解释。由于我对 java 不是很有经验,我想了解为什么我会收到此错误。我是否以错误的方式使用它?这是图书馆的错误吗?我想念传递一些参数吗?如上所述,我已经使用普通的输出流解决了这个问题,这是一个提高我对 java 的理解的问题。

FileOutputStream fos = null; 
try{ 

   log.info(String.format("file length: %s",streamAttach.length)); 
   log.info(String.format("check File : %s",filename)); 
   File f = new File(filename); 
   if(f.exists()) 
    ...                        

   boolean done= f.createNewFile();//here comes the exception
   //nothing of the following happens 
   if(!done) 
       throw new NWSException("error creating file"); 
   log.info(String.format("file %s creato", nomeFile)); 

thank you in advance for any answer

提前感谢您的任何答复

采纳答案by Armin Sadeghi

I ran into this problem recently and found that java.io.File.createNewFile() actually requires the "Change Permissions" permission (you can find this entry under Security->Advanced when checking folder permissions). Without this it will create the file and then subsequently throw an IOException.

我最近遇到了这个问题,发现 java.io.File.createNewFile() 实际上需要“更改权限”权限(检查文件夹权限时,您可以在“安全”->“高级”下找到此条目)。如果没有这个,它将创建文件,然后抛出一个 IOException。

It's deceptive because you will still be able to create files on the folder when manually testing, however createNewFile() will still fail if it doesn't have this particular permission (presumably such that it can change the permissions on the file its creating).

这是具有欺骗性的,因为手动测试时您仍然可以在文件夹上创建文件,但是如果 createNewFile() 没有此特定权限(大概是这样它可以更改其创建的文件的权限),它仍然会失败。

回答by oluies

If you are using Netapp that shares an NTFS (CIFS) style filesystem to Unix you could be experience "NFS is not allowed to change permissions on a file in an NTFS-style security volume." (TR-3490page 16)

如果您使用与 Unix 共享 NTFS (CIFS) 样式文件系统的 Netapp,您可能会遇到“不允许 NFS 更改 NTFS 样式安全卷中文件的权限”。( TR-3490第 16 页)

Options here are to change to a unix filesystem or set the cifs.ntfs_ignore_unix_security_opsflag to on for the file system which quiches the NFS permission error.

此处的选项是更改为 unix 文件系统或将cifs.ntfs_ignore_unix_security_ops标志设置为 on 以消除NFS 权限错误的文件系统。

java.io.UnixFileSystem.createFileExclusively(Native Method)opens the file with the O_EXCL and 0666 umask so I would get a EACCES, which really was a NFS3RR_ACCES

java.io.UnixFileSystem.createFileExclusively(Native Method)用 O_EXCL 和 0666 umask 打开文件,所以我会得到一个EACCES,这真的是一个NFS3RR_ACCES

open("/net/storage01-a/filer/myfile", O_RDWR|O_CREAT|O_EXCL, 0666) Err#13 EACCES

Also you can use OutputStream to create the file, that does not use O_EXCL it seemes

您也可以使用 OutputStream 创建文件,它似乎不使用 O_EXCL

回答by AlexR

It definitely not Java specific problem. If this Unix folder is mapped to your windows try to open file explorer and create file in this directory. I believe that you will get permission denied too. In this case fix this problem or ask your system administrator to help you.

这绝对不是Java特定的问题。如果此 Unix 文件夹映射到您的 Windows,请尝试打开文件资源管理器并在此目录中创建文件。我相信你也会被拒绝。在这种情况下,请解决此问题或请系统管理员帮助您。

Good luck!

祝你好运!