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 Where to store image files in Java web app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
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: 650
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: 61451
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by 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: 61451
    
  67

If the database is a bottleneck, then no, I wouldn't do that.
Alok Baba
Greenhorn

Joined: Oct 05, 2013
Posts: 5

Hi Milton,
I know its very old post..but i am also struggling with same scenario. I don't want to store images in DB , is there any good approach you have found for this problem?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

Please detail your problem exactly.
Alok Baba
Greenhorn

Joined: Oct 05, 2013
Posts: 5

Hi Bear, Thanks for your reply.
I figuring out which is the best way to store uploaded images (something like profile pic) in Java web app so that it is secured and we can access it in optimized way?
Storing images in DB takes more time to access. Storing images in WEB-INF is good idea? ..Kindly suggest me the best approach.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1189
    
    1

I would say .. store the profile pictures on the file system and profile picture locations [file path] in the database.
Saving picture in File system will always be beneficial because your images can be served from Web Server as Static content. Caching feature can be used on file system. DB query to get profile picture path shall be easier and quicker.


Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
Alok Baba
Greenhorn

Joined: Oct 05, 2013
Posts: 5

Thanks Abhay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where to store image files in Java web app