简体   繁体   中英

Uploading files to server and storing files to disk with Spring Framework 3

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM