简体   繁体   English

使用Spring Framework 3将文件上传到服务器并将文件存储到磁盘

[英]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. 我需要将文件上传到服务器,而我已经成功完成了那部分,但是现在我需要知道什么最佳实践是使用Spring将与数据库项相关的文件保存到磁盘上,以及如何使用Spring实际完成。

I thought it could be good way to use database table id in folder for example: 我认为这可能是在文件夹中使用数据库表ID的好方法,例如:

context_path/table_name/id/filename.file

There are three options: 共有三个选项:

  • store them in the database as blobs 将它们作为Blob存储在数据库中
  • 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 将它们存储在Web应用程序外部的文件夹中(并可在应用程序属性中配置),并保留对数据库中子文件夹+文件名的引用(在db中)
  • 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. 您将文件保存在数据库中具有该行ID的目录中的方法非常好。 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". 没有什么好方法可以使此100%事务真正安全(文件系统并不真正理解“事务”或“两阶段提交”的概念。

Just make sure that the context_path is configurable so the data can be moved around easily. 只要确保context_path是可配置的,就可以轻松地移动数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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