struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject Re: Where to store and how to retrieve Images for web app
Date Wed, 01 Dec 2004 16:56:17 GMT
I have faced this situation on a number of ocassions.  Speaking as someone who has done it
just about every which way, I offer this "best practice"...

I suggest storing them on the file system OUTSIDE the webapp directory.  I then suggest creating
an action specifically for returning images.  All you need to do in your JSPs are have normal
<IMG> tags, but the src is your action, i.e., something like:

<img src="/app/">

The action itself to return the images is really pretty trivial... Open the file, read it
in and send it along to the ServletOutputStream of your Response object, returning null as
the ActionForward from the Action so as to not forward anywhere since the response is complete
at that point.

I make this suggestion for a couple of reasons...

(1) Storing and retrieving from a database, which I've done, is a little bit more complicated,
but not terribly so.  More importantly though, dealing with BLOBs will usually lead to RDBMS-specific
code.  I try to avoid that whenever possible.  However, this approach is good if you have
restrictions (as I did in one case) in a host environment where they don't want you writing
to the local file system.  In any case, it's obviously more overhead per call.

(2) You don't want to store the file in the webapp directory, as another poster stated, because
then you have to remember to replicate the files when you need to blow away the app and reinstall.
 Why add that hassle?

(3) Doing it with an Action like this gives you a tad more control over how things work than
just figuring out a way to let the web server in front of the app server (or the app server
itself) serve the images.  You can pull that off of course, but the really pretty small amount
of code involved gives you some added flexibility (i.e., you can send in some sort of encoded
ID for the image and get a real filename out of that, if security is a big concern, etc.).

Hope that helps!

Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies

On Wed, December 1, 2004 9:23 am, said:
> My web app will have about 300 users, each of whom will be allowed to
> upload a max of 5 images/digital pictures.  I am using the Struts-upload
> package.  I considered storing the images in MySQL as blobs, but have read
> lots of advice that this is more trouble than it's worth / overkill.
> However, I am perplexed (I am a newbie) as to where I should store them on
> the file system and then how I would retrieve them and get them displayed
> in a browser.  What is a "best practice" for this?
> It would be easy to store them in a subdirectory of the web app, which
> would allow me to include them in my JSPs with a simple HREF tag. 
> However,
> does this cause problems with the size of the web app, maybe the start-up
> and reload speeds of the container, etc.?  I trialed this by storing 1500
> images in myWebApp/Pictures, starting Tomcat and retrieving images with a
> HREF tag in a JSP.  I didn't notice any impact on startup time or
> performance, but I want to make sure I'm not missing some other problem...
> If I store them in the filesystem outside of the web app directory
> structure, I can't just refer to them in my JSPs with an HREF tag, right?
> It would be more work to have an action servlet retrieve the image file
> and
> return it to the browser in the response object.  That seems to be just as
> much work as storing them in MySQL, so why not just use MySQL?   What are
> your opinions?
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

View raw message