Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image Uploading Problem In Tomcat Live Server

 
Sanjib Pal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have facing a problem such that when I am testing my project in local machine image is uploading successfully, but when I deployed the project in client server image not uploading. Is there any problem regarding file permission?

I have deployed the as a WAR file in the server.

My servlet code is as



My File Uploader code is as



Please help me.

Thanks in advance,
Sanjib
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line:

new FilePermission(uploadPath, "read, write, execute, delete")

accomplished nothing. A web app can't grant itself permissions that the JVM isn't granting to it.

Is that the problem, though? You didn't mention what's happening in the code, so this is just speculation.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18024
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never upload files into the Tomcat WAR directory subtree.

First of all, according to the J2EE standard, WARs aren't unpacked, and Java's ZIP IO API doesn't support replacing/adding single files in a WAR. So that approach only works on appservers which explode WARs. And even Tomcat only explodes WARs under certain conditions.

Secondly - and here I speak from bitter experience - if you upload files into a WAR subdirectory and someone upgrades the webapp, those files are going to get NUKED. All gone. Bye-bye. A WAR isn't something that's upgradable in parts, it's all or nothing. It's not a data directory and it was never intended to serve as a data directory. Just because you (sometimes) can access elements in the WAR as files and directories, doesn't mean that you should. Treat them as read-only. Because for practical purposes, they should be.

Always upload to a directory external to Tomcat and its supporting directories. Or expect the consequences.
 
Sanjib Pal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always upload to a directory external to Tomcat and its supporting directories. Or expect the consequences.


Could you please specify the path where to upload outside the war file ? I got confused what would be the path

Thanks,
sanjib
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18024
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanjib Pal wrote:
Always upload to a directory external to Tomcat and its supporting directories. Or expect the consequences.


Could you please specify the path where to upload outside the war file ? I got confused what would be the path

Thanks,
sanjib


Anywhere you want, just as long as it's outside of Tomcat and its webapps directory subtrees. In Linux/Unix, a popular convention is to create a directory under /var/lib and use that. In Windows, there's no convention at all. You can create a "C:\var\lib\uploads", if you like, or use any other directory that makes you happy.

For greatest flexibility, I recommend creating a web-inf resource to hold your upload directory path. The app code can then do a lookup of that resource to get the path. That makes your code portable and gives you options like using an alternate directory to upload into for testing purposes, since Tomcat can override the path you code in your web.xml using a Context resource definition.
 
Sanjib Pal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

That's fine I have got your point. Just to say could you give me an example how can I create a web-inf resource and use the resource for upload. One more thing if i upload the files in say "C:\var\lib\uploads" in this folder then how can I display it from my jsp page/ I mean like <img src=? />. So please explain with an example, it would be very helpful for me.

Thanks in advance,
Sanjib
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18024
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You can do a JNDI lookup on "java:comp/env/imagepath" to get the path.

Displaying the images is the harder part. Since a web server isn't a file server, something has to decode your image request URL, locate the image, and emit its content as an HttpServletResoponse data stream.

If you do a little searching, you can probably find a servlet like that already written that can be adapted (you'd need to put in a JNDI lookup for the image directory path). Then again, it usually takes less than half an hour to write a simple open-a-file-and-copy-it servlet.
 
Sanjib Pal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,
Thanks once again. I will try it.

Sanjib
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic