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.
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
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
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.
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.
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 certified Java 8 Programmer I (1Z0-808), 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