在java Web应用程序中,我应该在哪里存储用户的照片? [英] In java web application, where should i store users photos?

查看:121
本文介绍了在java Web应用程序中,我应该在哪里存储用户的照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能是愚蠢的,但我真的不明白如何解决它:
以免在我的应用程序中有一个用户。该用户编辑他的个人资料,并需要编辑他的头像。

this questions may be stupid, but i dont really see how to resolve it : lest say that in my application, i have a user. This user edit his profile, and need to edit his avatar.

我应该在哪里存储头像文件?
首先我正在保存src\main\webapp\resources中的所有文件,但每次我重新部署该文件夹清空。

Where should i store the avatar file? first of all i was saving all the files in src\main\webapp\resources , but each time i redeploy that folder empties.

所以我决定放在另一个位置:c:\wwwdir\resources,但是我无法将本地资源与远程页面链接,因此我无法显示任何头像。

so i dedide to place in an other location : c:\wwwdir\resources, but i can't link local resources from remote pages, so i was not able to display any avatar .

任何想法?建议?链接?

any idea? advise? link?

推荐答案

在解决之前需要考虑的事项:

Things to consider before coming to a solution:


  • 您是否会水平扩展您的webapp(即将运行多个servlet容器实例)。

  • 您期待什么样的流量?

  • 图像需要更新(即用户更改头像后,如果他们和所有其他用户立即看到新头像)。

对于没有水平缩放的webapp,可以使用文件系统作为图像存储。这是简单,可理解和有效的。

For a webapp that is not horizontally scaled, you can use the file system as your image store. This is simple, understandable, and works.

对于水平缩放的webapps,您需要将图像存储在每个servlet容器可以到达的位置。这可能是一个数据库(我不推荐这个),S3(我推荐这个)或一个内容存储库(我从来没有使用过这些)。 S3的一个优点是它能够很好地扩展。你可以把它放在CloudFront(亚马逊的CDN)后面,或者简单地把它从S3上卸载下来,并且把你的服务器关掉。

For webapps that are horizontally scaled, you will need to store your images in a place that each servlet container can reach. This could be a database (I don't recommend this), S3 (I recommend this), or a content repository (I've never used one of these). One advantage of S3, is that it scales well. You can put it behind CloudFront (Amazon's CDN), or simply server off of S3 and keep the load off of your servers.

您还提到您的问题,您无法从远程客户端服务器本地资源。这是正确的,有种...我猜想你试图使用一些URL,如 file:// c:/.../image.jpg 。这不行。您需要做的是映射一个处理程序来提供图像。这可能看起来像

You also mention in your question that you can't server local resource from remote clients. This is correct, kind of... I'm guessing you're trying to use some URL like file://c:/.../image.jpg. This won't work. What you need to do is map a handler to serve up the image. This might look something like

@RequestMapping(value = "/image/{name}.jpg", method = RequestMethod.GET)
public void image(@PathVariable("name") String name, HttpServletResponse response) {
    // Read the image from the file system using java.io
    // Write the image to the response output stream
    // Even better use org.springframework.utils.FileCopyUtils
}

这篇关于在java Web应用程序中,我应该在哪里存储用户的照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆