Where to store image files in Java web app

J

jsguru72

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.
 
A

Arne Vajhøj

jsguru72 said:
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.

I my best opinion that is the way to go.

Arne
 
L

Lew

jsguru72 said:
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. [sic]
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.

Why do you think data don't belong there? I see no problem with that approach.
2) The web application is huge because it is bloated with all of these
image files.

If those image files are going to exist anyway, the "bloat" is non-existent -
it takes the same amount of space to store them in one directory as another.

Your current structure doesn't seem so bad.
 
J

jstorta

The bloat is application bloat. The application WAR is approaching
1GB in size. Of that, maybe 1MB is the application and the rest of
the space is the image files.

Every time I modify the app and have to redeploy it, it has to copy
and extract all of those image files even though they never change.
This adds significant time to the redeploy process.

Not to mention, transferring the large WAR file across a network to
the production system is quite slow because of the size.

I look at it the same way as if someone said you have to store all of
your Excel files inside the Excel application folder. That would be
crazy. Here I have images that are opened by my web app, but they are
not part of the app. I think I should be able to store them anywhere
I want. I just need to figure out the best way to do it.

Thanks.
 
L

Lew

jstorta said:
The bloat is application bloat. The application WAR is approaching
1GB in size. Of that, maybe 1MB is the application and the rest of
the space is the image files.

Every time I modify the app and have to redeploy it, it has to copy
and extract all of those image files even though they never change.
This adds significant time to the redeploy process.

Not to mention, transferring the large WAR file across a network to
the production system is quite slow because of the size.

I look at it the same way as if someone said you have to store all of
your Excel files inside the Excel application folder. That would be
crazy. Here I have images that are opened by my web app, but they are
not part of the app. I think I should be able to store them anywhere
I want. I just need to figure out the best way to do it.

My point is that if you need those images in the new deployment, you'd have to
transfer them anyway. If you don't, then you don't need to transfer them even
if they're in a WEB-INF/ subdirectory.

If you don't want the images in the redeployment, why are you including them?

Web apps are not like Excel or other local applications. You are applying a
false analogy. Web applications are different.

The problem with using a server-local file system outside the application tree
of a Web app is that it is not portable. You are only safe using
subdirectories in the application context. You should not depend on anything
outside of that in the web app.
 
A

Arne Vajhøj

Lew said:
My point is that if you need those images in the new deployment, you'd
have to transfer them anyway. If you don't, then you don't need to
transfer them even if they're in a WEB-INF/ subdirectory.

If you don't want the images in the redeployment, why are you including
them?

It seems as if the images are data not application.

And data does not belong with the app.
The problem with using a server-local file system outside the
application tree of a Web app is that it is not portable. You are only
safe using subdirectories in the application context. You should not
depend on anything outside of that in the web app.

For a product that need to support multiple OS and multiple app
servers without problems that is very good advice.

If it is for a custom solution, then using a dir outside the app
server may work fine. If the company changes OS or app server, then
checking this is part of the migration work.

Arne
 
W

Wojtek

jsguru72 wrote :
Any thoughts or ideas would be appreciated.

Create a second application directory within webapps. Your application
writes to and refers to this second application. The application
contains nothing except images:

webapp/myapp
webapp/images

You update "myapp" with your latest code. All the images are in
"images", and so the update is only code.

You will need a web.xml file so that Tomcat recognizes the "images" as
a place it serves files from.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top