• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Image Uploading Problem In Tomcat Live Server

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 28420
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 28420
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 28420
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,
Thanks once again. I will try it.

Sanjib
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic