aspose file tools
The moose likes Servlets and the fly likes Where to store image files in Java web app Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Where to store image files in Java web app" Watch "Where to store image files in Java web app" New topic
Author

Where to store image files in Java web app

Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

Hi Ranchers

I have a personal web site that I use to view family pictures and
such. I have a database that stores the file name and I keep all of
the images in a sub-directory under WEB-INF so that they are not
directly accessible. I then use a servlet to check for a valid login
and forward the request to the WEB-INF directory.

I.E.
Images stored in WEB-INF/private
User requests /myapp/image/fileabc.jpg
Servlet mapped to 'image' checks for valid login, then forwards
request to WEB-INF/private/fileabc.jpg

This works okay, but I have 2 problems with this solution.
1) I am storing data in WEB-INF, where it really does not belong.
2) The web application is huge because it is bloated with all of these
image files.

To me, the images are data and should be stored somewhere else. The
problem is I cannot find an existing solution that lets me load an
image from outside the application context.

I would like to store the images in something like D:\images and then
use a servlet to get the filename from the database and return the
image from the D:\images path.

The only solutions I have seen are to write a servlet that streams the
image file to the web page. I can go this route, but I wasn't sure if
there was a better alternative. To me anyway, this seems like a
problem that would be fairly common. I mean Flickr and YouTube
certainly do not store all of their media within the application
directories.

There may be something really obvious that I am overlooking so before
I start writing a servlet to stream my images, I figured I would run
this by the experts.

Any thoughts or ideas would be appreciated.


Thanks.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 639
Creating an image handler servlet seems like the right way to go. This servlet can even check permission if you want.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56155
    
  13

Yes, I would not store the images inside the web app. Makes for all sorts of nasty problems.

Store them elsewhere in the file system and use a servlet, as Mark suggested, to serve them.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

Thank you for you answer.

Before that solution, I was storing the images on a Database MySQL, but I found that when I retrieve this images (for a thumbnails) took too long to display the JSP.

Definitive It's not a good a idea to store images on a database. am I right?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56155
    
  13

If the database is a bottleneck, then no, I wouldn't do that.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Where to store image files in Java web app
 
Similar Threads
What is best way to name jpg images in a web application?
how to handle file uploads in spring
architectural help required
How to get upload an image on server and get it on next jsp?
How to know img tag src attribute loaded completely by action