This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Aleks Kushch
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's already been moved; you don't need to do anything. IIRC == If I Remember Correctly.
 
Aleks Kushch
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic