简体   繁体   English

在Spring MVC Hibernate应用程序中,应该将用户图像保存在哪里?

[英]In a Spring MVC Hibernate application, where should I save user images?

I am creating a Spring MVC Hibernate application using MySQL. 我正在使用MySQL创建Spring MVC Hibernate应用程序。 Where should I save the User Images: in the database or in some folder, like under WEB-INF/ ? 我应该在哪里将用户映像保存在数据库中或某个文件夹中,例如WEB-INF/

Certainly not inside WEB-INF. 当然不在WEB-INF中。 You might want to save them in the file system, but not in the webapp's directory. 您可能希望将它们保存在文件系统中,而不是保存在webapp的目录中。 First of all, it could very well be inexistent, if the app is packaged as a war. 首先,如果应用程序打包成一场战争,它很可能不存在。 And second, you would lose evrything as soon as you redeploy the app. 其次,重新部署应用程序后,您将一无所获。 Desktop apps don't store their user data in their install directory, do they? 桌面应用程序不会将其用户数据存储在其安装目录中,对吗? It should be the same for webapps. 对于webapp,应该相同。

Now, since images are usually big, and they're not searchable, you might want to store them on the file system, and only store their name, path, hash, and/or mime type into the database. 现在,由于图像通常很大,并且无法搜索,因此您可能希望将它们存储在文件系统上,而仅将其名称,路径,哈希和/或mime类型存储到数据库中。 But it depends on your application, if they need to be served/used by other applications, if these apps have access to the database and/or the file system, etc. You decide. 但这取决于您的应用程序,是否需要由其他应用程序提供服务/使用它们,这些应用程序是否有权访问数据库和/或文件系统等。您可以决定。

You can choose it: 您可以选择它:

  1. DataBase - you have the positive point that this can be associated with records and will never be orphan (depending on your model). 数据库 -您有一个积极的一点,那就是它可以与记录关联,并且永远不会孤立(取决于您的模型)。 For backup it is a little bit painful for situations in which the database increases. 对于备份,在数据库增加的情况下会有些痛苦。

  2. FileSystem - backup facility, as these are physical files, an rsynch process should be enough. FileSystem-备份工具,因为它们是物理文件,所以rsynch进程就足够了。 Another positive point is that you reduce the IO from DB. 另一个好处是您减少了数据库的IO。 Although, it is quite hard to attach a logic between the files and the record stored in the DB (you have things distributed), so you will not be sure if the file wasn't removed and there are still some records referring to it in DB. 虽然,很难在文件和存储在数据库中的记录之间附加逻辑(您已经分发了东西),所以您将不确定是否未删除文件,并且仍然有一些记录引用该文件。 D B。

If filesystem option is chosen, put it outside the application directory structure (prepare a dedicated place for the files) . 如果选择了文件系统选项,请将其放在应用程序目录结构之外(为文件准备专用的位置) The application dir should not be modified, causing some pain when redeployment is done. 不应修改应用程序目录,以免在重新部署后造成麻烦。 You can use symbolic links though. 您可以使用符号链接。

With images, probably you want to perform some thumbnails and so on, this would be cheaper using FileSystem option. 对于图像,可能要执行一些缩略图等操作,使用FileSystem选项会更便宜。

That depends what you're trying to accomplish. 这取决于您要完成的工作。
If these are static images , and you have a fixed number of users ,you can consider saving them under WEB-INF/. 如果这些是静态图像,并且您有固定数量的用户,则可以考虑将其保存在WEB-INF /下。
However, most likely this is not you case, and you have varying number amount of users and you have to store a user for each one of them. 但是,很可能不是您这种情况,并且您拥有数量不定的用户,并且必须为每个用户存储一个用户。

Possible solutions: 可能的解决方案:
A. For the user store an image name, and have a convention of storing/loading from a well known directory in your file system. A.为用户存储映像名称,并具有从文件系统中的知名目录进行存储/加载的约定。
B. Store the image as a blob in your DB. B.将图像作为Blob存储在数据库中。 Consider checking the @Lob annotation 考虑检查@Lob批注

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

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