I have a Java web application running on Tomcat. I want to load static images that will be shown both on the Web UI and in PDF files generated by the application. Also new images will be added and saved by uploading via the Web UI.
It's not a problem to do this by having the static data stored within the the web container but storing and loading them from outside the web container is giving me headache.
I'd prefer not to use a separate web server like Apache for serving the static data at this point. I also don't like the idea of storing the images in binary in a database.
I've seen some suggestions like having the image directory being a symbolic link pointing to a directory outside the web container, but will this approach work both on Windows and *nix environments?
Some suggest writing a filter or a servlet for handling the image serving but those suggestions have been very vague and high-level without pointers to more detailed information on how to accomplish this.
I did it even simpler. Problem: A CSS file had url links to img folder. Gets 404.
I looked at url, http://tomcatfolder:port/img/blablah.png, which does not exist. But, that is really pointing to the ROOT app in Tomcat.
So I just copied the img folder from my webapp into that ROOT app. Works!
Not recommended for production, of course, but this is for an internal tool dev app.
If you want to work with JAX-RS (e.g. RESTEasy) try this:
using
javax.ws.rs.core.Response
andcom.google.common.io.ByteStreams
This is story from my workplace:
- We try to upload multiply images and document files use Struts 1 and Tomcat 7.x.
- We try to write uploaded files to file system, filename and full path to database records.
- We try to separate file folders outside web app directory. (*)
The below solution is pretty simple, effective for requirement (*):
In file
META-INF/context.xml
file with the following content: (Example, my application run athttp://localhost:8080/ABC
, my application / project namedABC
). (this is also full content of filecontext.xml
)(works with Tomcat version 7 or later)
Result: We have been created 2 alias. For example, we save images at:
D:\images\foo.jpg
and view from link or using image tag:or
(I use Netbeans 7.x, Netbeans seem auto create file
WEB-INF\context.xml
)if anyone not able to resolve his problem with accepted answer, then note these below considerations:
localhost:<port>
with<img> src
attribute.context docBase
entry on its own inside its localserver.xml
file.