This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Tomcat and the fly likes Image Uploading Problem In Tomcat Live Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Image Uploading Problem In Tomcat Live Server" Watch "Image Uploading Problem In Tomcat Live Server" New topic
Author

Image Uploading Problem In Tomcat Live Server

Sanjib Pal
Greenhorn

Joined: Feb 08, 2010
Posts: 27
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


SCJP 5.0
Try unless you get the ultimate success.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
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.


Ping & DNS - my free Android networking tools app
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Sanjib Pal
Greenhorn

Joined: Feb 08, 2010
Posts: 27
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

Joined: Jun 25, 2001
Posts: 16022
    
  20

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

Joined: Feb 08, 2010
Posts: 27
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

Joined: Jun 25, 2001
Posts: 16022
    
  20



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

Joined: Feb 08, 2010
Posts: 27
Hi Tim,
Thanks once again. I will try it.

Sanjib
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Image Uploading Problem In Tomcat Live Server