File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Access files outside the context of the Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Access files outside the context of the Servlet" Watch "Access files outside the context of the Servlet" New topic

Access files outside the context of the Servlet

Bageshwar Pratap Narain

Joined: Mar 04, 2009
Posts: 24
I want to access files which are outside the webapps directory.
My OS is windows/linux and I have a scenario, wherein, i need to write files, to specific directories, outside webapps.
Tomcat throws a Security Exception, so how do I get away with this.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
Sounds like Tomcat is running with a security manager that disallows access to that file. Does the account Tomcat is run under allow access to that file, and to all directories above it?
veda bandaru

Joined: Sep 14, 2006
Posts: 3

Could you post the code where you are trying to access the file.

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13025
For security, servlet containers are prohibited from serving directly files outside the context. Tomcat's "default" servlet handles file requests such as css and image requests but obeys this prohibition.

The solution is simple, you must provide your own servlet implementation that gets the image request, knows where to look for (for example) image files, creates the correct headers, and send the data.


I agree. Here's the link:
subject: Access files outside the context of the Servlet
It's not a secret anymore!