views:

379

answers:

5

In the PHP world it's common to create a directory called 'uploads' or something similar in the application folder. All uploaded files are then stored there, the database containing the relative paths to those files.

In the Java/servlet world however, I'm not sure what the common way to handle uploaded files is. Getting the path to a directory inside the application directory can be difficult, and the entire directory could be destroyed if the WAR file is redeployed. At the moment I'm storing uploaded files in the database as a blob, but this is not my preferred solution.

So my question is, where should I store uploaded files?

+2  A: 

In any folder on the disk that doesn't belong to your app's file structure nor the application server/container server. You may keep the absolute path to this folder/folders as a configuration in a file or database.

Boris Pavlović
+1  A: 

Why not store in a directory outside the .war hierarchy ? Specify a directory in a servlet parameter (to make it configurable), and check/create on servlet initialisation (so things don't first blow up when someone uploads a file for the first time).

Brian Agnew
A: 

Do the same thing; create an uploads folder somewhere on the filesytem and stick the files in there. Don't put it in your web container as it will get removed if your WAR is undeployed.

banjollity
A: 

like Brian and Boris mentioned. Why not store the uploaded file outside your WAR directory. Why is it difficult to get a path outside your application directory.

Omnipresent
A: 

Save them to a blob field in the database - any modern database is more than capable of dealing with this.

My one exception would be if you're are serving the files with high frequency when there's performance gains to be made by handled the requests outside the servlet container.

Nick Holt