You could use a web.xml centext-param to pass the absolute path to the servlet.
 
In my experience the getRealPath()-method more often than not returns null. If you want to server resources from
a jar then use weblets. If you want to serve them from the filesystem use an absolute path configured via a
system-setting of a context-param.
 
hth
Alexander


From: Quintin Kerby [mailto:[EMAIL PROTECTED]
Sent: Friday, April 07, 2006 11:50 PM
To: MyFaces Discussion
Subject: Re: [OT] Storing files outside of the web context


Absolutely.  You could hard code a file path from inside a backing bean and save the file anywhere on your system if you wanted.

Here's some quick code I stole from one of my servlets that reads a file from anywhere (just define your own filename) and sends it to the response:

ServletContext sc = getServletContext();
String filename = sc.getRealPath("/images/bcg.gif");

// Get the MIME type of the image
String mimeType = sc.getMimeType(filename);
if (mimeType == null) {
        mimeType = "text/plain";
}

// Set content type
response.setContentType(mimeType);

// Set content size
File file = new File(filename);
response.setContentLength((int) file.length());
               
//Set headers
response.setHeader("Content-disposition", "attachment; filename=" + filename);
response.setHeader("Cache-Control", "max-age=600");

// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();

// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
        out.write(buf, 0, count);
}
in.close();
out.close();

Quintin Kerby
CACI, Inc.



"James Reynolds" <[EMAIL PROTECTED]>

04/07/2006 15:02

Please respond to
"MyFaces Discussion" <[email protected]>

To
"MyFaces Discussion" <[email protected]>
cc
Subject
[OT] Storing files outside of the web context






I have a jsf app that includes many .pdf files of contracts.  I've just
been putting them all in the war before deploying to the production
server.  There must be a better way to do this.  Is it possible to store
these files elsewhere on the web server, outside of the context, and
still be able to link to them?

Thanks



Reply via email to