java.io.IOException: Permission denied on network folder

user260192 picture user260192 · Feb 9, 2011 · Viewed 18k times · Source

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 :

Permission denied java.io.IOException: Permission denied at 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.

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.

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

Answer

Armin Sadeghi picture Armin Sadeghi · Sep 5, 2011

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.

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