File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Accessing Uploaded Images in a System Folder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Accessing Uploaded Images in a System Folder" Watch "Accessing Uploaded Images in a System Folder" New topic

Accessing Uploaded Images in a System Folder

Abdullah Dokuyucu

Joined: Apr 27, 2010
Posts: 24

I upload images to a folder on the server's hard drive. Next I would like to display them back to the user. My question is; is it possible to map a system folder to a context relative path? And how to it? Entry in web.xml perhaps? ie. from "C:\img\" to "/resources/image/" (Of course, I could open the files in Java and stream myself but why do that when the container can do it?)

Alternatively, would you recommend storing images in a database and stream from there instead.

Thank you for taking time to answer a really basic question, my regards.

Freelancer for life!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417


Absolutely do NOT upload anything to your WAR directories! It's not guaranteed to work in all environments and even where it does, the first time you update the WAR all your uploaded files will get nuked. Always upload to an external directory outside of the webapp server environment. Preferably one whose path can be configured via a deployment context definition and not hard-coded into the application. Or, like you said, upload to a database or something.

A web server is not a file server. So you will need a simple servlet to copy files from your image repository back out to the remote client. I'm afraid there's no standard copy-file servlet, but it's a simple matter to create one.

As to whether it's better to keep images in files or as BLOBs in databases, you can hear plenty of argument over that one. Files have the advantage of being easier to access. However, if you have database records referencing the files, keeping everything in one place reduces problems where the 2 repositories get out of sync.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Accessing Uploaded Images in a System Folder
jQuery in Action, 3rd edition