Reputation: 7568
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?
Upvotes: 3
Views: 4901
Reputation: 2847
How about creating a servlet that reads your local resource from wherever you've stored it and just streams it back as the appropriate content type? So instead of <img src="/resources/xyz.jpg"/>
you'll have something like <img src="/resourceservlet?id=xyz.jpg"/>
. So you can then store the files in your C:\wwwdir\resources folder as you want to do.
Searching around the web for a "java image servlet" will give you some links as to how to do this. You can use one of these custom servlets or as mentioned in another response use the facilities of a framework, such as Spring, to build on.
You could also consider storing such graphics over in Amazon S3 but I don't know the scale of the thing you're building. Having that servlet in place lets you move the content around without having to redo your page links.
Upvotes: 3
Reputation: 105063
How about plain old MySQL?
Another option for avatars would be to use gravatar.com (or something similar). This approach is much more modern and effective than keeping avatars in-house.
Upvotes: 0
Reputation: 4385
Things to consider before coming to a solution:
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
}
Upvotes: 7