my dog learned polymorphism*
The moose likes Servlets and the fly likes Serving files outside document root Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Serving files outside document root" Watch "Serving files outside document root" New topic
Author

Serving files outside document root

Erik Pragt
Ranch Hand

Joined: Sep 08, 2001
Posts: 125
Hello all,

I have a quite simple question which I have a hard time solving. People can upload files (images) to my site. I'd like to display the images to the visitors of the site, but I do not want to store the images in the document root, but outside of it.

Does anyone know how to accomplish such a task? Do I have to work with different RequestDispatchers, or is there some other way?

Thanks, Erik
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

It's not difficult to write a servlet that will read the file off the disk and send the contents to the response output.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Erik Pragt
Ranch Hand

Joined: Sep 08, 2001
Posts: 125
Hello Bear,

Thanks for your reply. Probably it's not so hard, but is it the only way, or are there other ways to do so?

Erik
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
think about it.
If a webserver could serve files from any location on the system it's running on that would be a massive security leak.

Many application servers won't even allow servlets (or PHP, CGI, whatever) to access any files outside their application scope without special settings in the server configuration for that very reason.


42
Erik Pragt
Ranch Hand

Joined: Sep 08, 2001
Posts: 125
Jeroen, I agree with you, it could be a security hole, but this could maybe be prevented by the Security Manager or the file system.

Maybe you have a suggestion than how to handle file uploads and be able to present the upload files to other users?

Any help would be greatly appreciated, and thanks for your suggestions so far.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Do as bear suggested. A similar approach can work for uploading as well.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serving files outside document root