Skip to main content

where to store uploaded files

3 replies [Last post]
Joined: 2010-06-06


I have a application which has two WARs, one WAR accepts user uploads of files and then display back to the users. The other WAR is required to access these files.

But if I put the uploaded files outside of the WAR context dir. it's hard to construct a URL for an uploaded file.

So what I want to ask is, what's the usual practice to handle this case by most webapp developers?

Thank you very much for help.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2010-06-06

Thank you.

You answered the question how to store uploaded file in any designated place outside WAR.

How to access that file form another WAR. I know Java Petstore 2.0 using Servlet retrieve that file. First Servlet gets the file name from URI, then write the file from the place saved to an output stream

Are there any better ways to retrieve files

Joined: 2010-01-26

Supposing you've got a byte stream of an inputted image, perhaps produced
by p:fileUpload, then you need to write it to a file or database in order for
h:graphicImage to display it. I have some code that will write to a disk file if
you're not using a database. Anyway, given the byte stream you need to put
it somewhere for graphicImage to display it. Use the return from loadFile
.getName() or whatever it is and you're in business.

[code]private static final String tempDir =

[code]public File loadFile(byte[] b) throws Exception {
File tempFile = File.createTempFile("dbload", ".jpg", new File(tempDir));
FileOutputStream fos = new FileOutputStream(tempFile);
return tempFile;


Joined: 2010-01-26

I 'gathered' this code from somwhere ages ago, apologies to the original author
can't remember where from, but thanks anyway.
public byte[] storeFile(File fileToLoad) {
InputStream is = null; try { is = new FileInputStream(fileToLoad); } catch (FileNotFoundException ex) { SQLog.log(ex.getMessage()); } long length = fileToLoad.length(); if (length > Integer.MAX_VALUE) { // File is too large } byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; try { while (offset < bytes.length && (numRead =, offset, bytes.length - offset)) >= 0) { offset += numRead; } } catch (IOException ex) { SQLog.log(ex.getMessage()); } return bytes; } </code> Message was edited by: healeyb