You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by James Reynolds <Ja...@intermountainmail.org> on 2006/04/07 21:02:07 UTC

[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


Re: [OT] Storing files outside of the web context

Posted by Quintin Kerby <qk...@caci.com>.
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" <Ja...@intermountainmail.org> 
04/07/2006 15:02
Please respond to
"MyFaces Discussion" <us...@myfaces.apache.org>


To
"MyFaces Discussion" <us...@myfaces.apache.org>
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