File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Open Source Projects and the fly likes Image upload problem when using Tomcat 6 and org.apache.commons.fileupload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Image upload problem when using Tomcat 6 and org.apache.commons.fileupload" Watch "Image upload problem when using Tomcat 6 and org.apache.commons.fileupload" New topic
Author

Image upload problem when using Tomcat 6 and org.apache.commons.fileupload

Aleks Kushch
Greenhorn

Joined: Oct 31, 2009
Posts: 5
I have hit rock bottom with this, please help

I am trying to upload images onto a shared hosting server (Tomcat 6), using multipart/form-data POST request and org.apache.commons.fileupload API.

After the program extract the images from the multipart/form-data, it writes them to disk using the following code:



config.getServletContext().getRealPath("/"+"Uploaded_Images") returns the follwoing path to which the file should be written to "/home/donnarosa/web-root/Uploaded_Images".
However tomcat throws the following error:



Why the hell is Tomcat 6 trying to write the file to /home/tomcat/default/temp/upload_18249544_124a245f512__7ff4_00000062.tmp, this is tomcat default temporary directory. I specifically tell it to write to "/home/donnarosa/web-root/Uploaded_Images". It is like it completely ignores the path that i specify.

I asked hosting support about this and that what they replied:

Looking at this line:

java.security.AccessControlException: access denied (java.io.FilePermission /home/tomcat/default/temp/upload_18249544_124a245f512__7ff4_00000020.tmp write)

it seems to me that you're trying to use the tomcat default temporary directory.
Your application is not allowed to write there as this is a security risk. You should use a directory which is under your home directory. E.g. create /home/donnarosa/tmp and do a
chmod a+w tmp



Any suggestions or help will deeply appriciated.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd guess it's commons-fileupload that's saving to the temp directory first.

http://commons.apache.org/fileupload/using.html#The_simplest_case
Aleks Kushch
Greenhorn

Joined: Oct 31, 2009
Posts: 5
David Newton wrote:I'd guess it's commons-fileupload that's saving to the temp directory first.

http://commons.apache.org/fileupload/using.html#The_simplest_case


How can i change that? I looked at that link, but the infomration thre is limited, to torubleshoot this problem.
Aleks Kushch
Greenhorn

Joined: Oct 31, 2009
Posts: 5
I think I am slowly beging to understand what is wrong.

According to apache commons:

What happens during the file upload is that the files are first stored temporarily on disk on the server. "Is this right?"

And the repository into which they are stored temporarily into, is set to system default temp directory.

In my case, the default repository will be "/home/tomcat/default/temp/upload_18249544_124a245f512__7ff4_00000062.tmp".
Thats why I get Access Denied error becasue it is a security risk to write to this folder and the hosting company blocks writing to this folder.

So David Newton was right at suggesting that commons-fileupload saves the files first to temp directory and then to the directory that you specify. I didn't realise that was the case.

So to solve the problem, we need to change the default repository dir from default to root location on the server:

First create tmp folder in you root dir.

Second change the repsitory using the following code:

DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File("/tmp"));

Now the file will be attempted to be writen to new temp dir.

I still get an error and cannot write to /tmp because there are now write permissions set for this folder, so I contacted hosting support to enable write permissions.

I post an update on mu progress soon..


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why not make the temporary directory something you can write to? Like your hosting account, or something in the web app (which might not work, depending on how you're deploying and other factors). IIRC you can also set this in a properties file.

I'm moving this to a more appropriate forum, as it's not really JSP-related.
Aleks Kushch
Greenhorn

Joined: Oct 31, 2009
Posts: 5
I'm moving this to a more appropriate forum, as it's not really JSP-related.


Can you give a link to where you moving this.

Why not make the temporary directory something you can write to? I dont know what you mean. If I create a tmp dir anywhere in root of my accounbt it seesm to need write permissions

What is IIRC?

Cheers,
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's already been moved; you don't need to do anything. IIRC == If I Remember Correctly.
Aleks Kushch
Greenhorn

Joined: Oct 31, 2009
Posts: 5
I managed to get around the problem. I had to set write permissions as 777 to temp folder in /home/donnarosa/tmp.

and also change factory.setRepository(new File("/tmp")); to factory.setRepository(new File("/home/donnarosa/tmp"));

It still doesnt work, the directories are written to the database, but the actual image file is not saving into /home/donnarosa/web-root/Uploaded_Images.

Any ideas?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If it's the same error, then you simply can't write files into your webapp. Note that putting images there is somewhat risky, depending on how your ISP operate--a redeploy might just delete them all. IMO uploaded images should be saved outside the webapp, and retrieved with an image servlet.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Image upload problem when using Tomcat 6 and org.apache.commons.fileupload