Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In java web application, where should I store users photos?

This question may be stupid, but I don't really see how to resolve it : let's say that in my application, I have a user. This user edit his profile, and need to edit his avatar.

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.

So I decided to place in another 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?

like image 624
storm_buster Avatar asked Dec 15 '25 12:12

storm_buster


1 Answers

Things to consider before coming to a solution:

  • Will you be horizontally scaling your webapp (i.e. will you be running multiple servlet container instances).
  • What kind of traffic are you expecting?
  • How quickly does the image need to update (i.e. after a user changes their avatar, should they and all other users see the new avatar immediately).

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

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.

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
}
like image 82
three-cups Avatar answered Dec 17 '25 01:12

three-cups



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!