Question

I have following environment setup.

Server: Ubuntu    
App Server: Tomcat7
Java: JDK 7

A servlet need to write a file into a mount folder. Tomcat is running using a user called tomcat7 and a CIFS is bound at /mntt/mydir/ with a different user called coder.

The permission is set to 777 for /mntt/mydir

Service will create a folder and try to upload/write a file into that folder.

When folder is created it has permission set to 755 and file has permission set to 644, owner of both file and folder is coder and not tomcat7

So, when my program tries to write something in file it throws Permission denined exception.

UPDATE File Creation Code

//Create directory
File uploadFile = new File(dirpath);
uploadFile.mkdirs();

//Create and Write to File
OutputStream stream = new FileOutputStream(new File(dirPath+"/upload"));

Please guide. Also, please let me know if you need further information.

Thanks

Was it helpful?

Solution 2

Finally i solved the problem !!

I run the tomcat service as coder user by modifying /etc/defaults/tomcat7.

Also, i have to change permissions of few tomcat directories.

OTHER TIPS

You can change the Java service as follows, so that the created file gets 777 permission, and the other user will be able to write to it:

    Path path = FileSystems.getDefault().getPath(
            file.getAbsolutePath());
    Files.setPosixFilePermissions(path,
            EnumSet.allOf(PosixFilePermission.class));

where file is the java.io.File object that represents the file. You can do the same thing to the directory that contains the file, if you want the tomcat user to be able to create files int that directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top