简体   繁体   English

如何在Java Web应用程序中存储文件?

[英]How to store files in a java web application?

This is a design related question. 这是一个与设计有关的问题。

I have a java web application(connected to MySQL database) running on tomcat. 我在tomcat上运行了一个Java Web应用程序(已连接到MySQL数据库)。 It runs on both linux and windows machines separately(as of now). 它分别在linux和Windows机器上运行(到目前为止)。 I should store files which come as input, whose size varies from 500 KB to 100 MB (max). 我应该存储作为输入的文件,其大小从500 KB到100 MB(最大)不等。 The use of these files is to parse them and get required data. 这些文件的用途是解析它们并获取所需的数据。 I can use the 'blob' in database schema to store files, access them to generate the required output and store them in MySQL db. 我可以在数据库模式中使用“ blob”来存储文件,访问它们以生成所需的输出,并将它们存储在MySQL数据库中。 Else, I can use file system to store the files in the windows or linux filesystem and use them to extract the data, store it in MySQL db. 否则,我可以使用文件系统将文件存储在Windows或linux文件系统中,并使用它们来提取数据,并将其存储在MySQL数据库中。 In any month I will be saving close to 200 files of varying length. 在任何月份中,我将保存将近200个不同长度的文件。 I am unable to decide which implementation to choose? 我无法决定选择哪种实现?

The following are some pros and cons which I came up comparing filesystem vs database. 以下是我比较文件系统与数据库的一些优缺点。

File-System: 文件系统:

  1. automated indexing of files by OS. 通过操作系统自动建立文件索引。
  2. easier to access files. 更容易访问文件。
  3. MySQL database that I use for other tables doesn't store huge amounts of data (200 files * 50 MB average = 10GB data). 我用于其他表的MySQL数据库不会存储大量数据(200个文件*平均50 MB = 10GB数据)。
  4. Varying lengths of the files doesn't matter much to the DB( the typical blob sizes for MySQL is, TINYBLOB: 255 bytes BLOB: 65,535 bytes (64 KB) MEDIUMBLOB: 16,777,215 bytes (16 MB) LONGBLOB: 4 GB So if my file size is >16MB I should store it as a LONGBLOB. which means for the size range I should be using LONGBLOB to store 500 KB file or a 100 MB file. 文件的不同长度对数据库无关紧要(MySQL的典型Blob大小为TINYBLOB:255字节BLOB:65,535字节(64 KB)MEDIUMBLOB:16,777,215字节(16 MB)LONGBLOB:4 GB因此,如果我的文件size> 16MB我应该将其存储为LONGBLOB,这意味着对于大小范围,我应该使用LONGBLOB来存储500 KB文件或100 MB文件。

MySQL: MySQL的:

  1. Faster access. 更快的访问。
  2. Single place for all the data. 所有数据的单一位置。 Data extracted from files and files are stored in one location. 从文件和文件中提取的数据存储在一个位置。
  3. .. ..

Read https://dba.stackexchange.com/questions/2445/files-in-the-database-or-not 阅读https://dba.stackexchange.com/questions/2445/files-in-the-database-or-not

This is a good article too: http://www.revsys.com/blog/2012/may/01/three-things-you-should-never-put-your-database/ 这也是一篇很好的文章: http : //www.revsys.com/blog/2012/may/01/three-things-you-should-never-put-your-database/

I think it depends on how much read/write activity you have. 我认为这取决于您有多少读/写活动。 I'm personally in favor of keeping these files in the database specially if you have a fast database like MongoDB. 我个人赞成将这些文件保留在数据库中,特别是如果您拥有像MongoDB这样的快速数据库。 You put a limitation of 100MG so it wouldn't get that large either. 您设置了100MG的限制,因此也不会太大。

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

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