views:

119

answers:

2

I need to upload files to server and that part I have successfully done, but now I need to know what is best practise to save files that are related to database items to disk with Spring and how that is actually done with Spring.

I thought it could be good way to use database table id in folder for example:

context_path/table_name/id/filename.file
+1  A: 

There are three options:

  • store them in the database as blobs
  • store them in a folder external to the web application (and configurable in the application properties) and keep reference (in the db) to the subfolder+filename in the database
  • push the items to some other storage, like a content repository, and keep reference (in the db) to the unique key of the resource

This is the case not only with spring, but with any application.

Bozho
+2  A: 

Your approach to save the file in a directory which has the name of the ID of the row in the database is pretty good. There is no good way to make this really 100% transaction safe (filesystems don't really understand the concept of "transaction" or "two phase commit".

Just make sure that the context_path is configurable so the data can be moved around easily.

Aaron Digulla
I think he successfully handle the upload
Bozho
Yeah, missed that. Updated my answer.
Aaron Digulla