File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Unable to view uploaded files in Tomcat till server is refreshed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Unable to view uploaded files in Tomcat till server is refreshed" Watch "Unable to view uploaded files in Tomcat till server is refreshed" New topic
Author

Unable to view uploaded files in Tomcat till server is refreshed

Akash Singla
Greenhorn

Joined: Jul 22, 2009
Posts: 27
Hi All,

I am building an application wherein the user would have an option to upload files (can be documents or images) and once the files are uploaded the user should be able to download it.

I am using Apache Tomcat 6.0 and JDK1.6.
Every thing seems to be OK for the File Upload part but I have some questions in my mind for the File Download Part.

Approach 1:
Initially I tried to use the BLOB feature of the mysql Database to save the file as a Binary stream in the DB. However after googling it out I found out that there are serious performance issues with BLOB (The file size can be anywhere from 1 KB to 20 MB). Thus we have ruled out this approach.

Approach 2:
We decided to save the file path in the DB and save the file to the File System.

For this, Initially we stored the files under the Root directory of the application but the Tomcat could not recognize the files until it was refreshed/restarted.
This can’t be accepted for the realtime environment.
One fix we found out for this was to create a new file directory under the web-apps folder of tomcat outside the root directory of the application and save the files in this directory. To make this work we added a new context to the tomcat configuration.

Please suggest if this is the best approach to follow.

Thanks
Akash Singla
http://www.TheDaedals.com

Member of TheDaedals Club
http://www.thedaedals.com/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

I do similar things often.

I don't store the files anywhere within the web app. That makes it hard to update and redeploy the web app because now it's a mix of the app and its data files. What a mess.

Rather, I store the files elsewhere on the file system. Then I allow access to them through a servlet that reads the files and streams them as the response.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

As there is nothing Tomcat-specific about this, it's been moved to the Servlets forum.
Akash Singla
Greenhorn

Joined: Jul 22, 2009
Posts: 27
Thanks Bear, we'll try it and let you know

Bear Bibeault wrote:I do similar things often.

I don't store the files anywhere within the web app. That makes it hard to update and redeploy the web app because now it's a mix of the app and its data files. What a mess.

Rather, I store the files elsewhere on the file system. Then I allow access to them through a servlet that reads the files and streams them as the response.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16061
    
  21

I got burnt very badly when I inherited a project that was storing images inside the WAR directory. First software upgrade, and "BAM!" All the images are gone.

Besides, you can't upload files into a WAR unless it's an exploded WAR. And exploded WARs actually aren't part of the J2EE architecture. They're a "feature" of some, but not all J2EE appservers. In fact, while some versions of Tomcat would automatically explode WAR files, some don't appear to, and it's a configurable option on most of the recent ones, I think.

It can be a problem, however, using an external data directory, since that codes a dependency on your server's filesystem into the app. A good way of getting around that is to define the directory path as a web.xml resource and look it up via JNDI. In Tomcat, you can override the default value in web.xml without actually having to modify web.xml itself. Although even that's better than having to change source code and recompile.

Blob avoidance is one of those Urban Legends. I wouldn't just assume that because someone says so that they're always going to be a bad solution, but rather do an app-specific case study. Obviously, as always, abstracting your storage mechanism will help in switching mechanisms once you find which way you want to go. And change that mechanism later, if it proves profitable.

Oracle 11 was claiming that database accesses were faster than file accesses, for example. YMMV.

One thing I do know from my own project. It can be a real pain when the images are in the filesystem when you're using the database on a separate test machine.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to view uploaded files in Tomcat till server is refreshed