1

I am working on a Spring-MVC application in which for the user, we are saving the thumbnail on the filesystem itself. Now for chat, I want to create a URL for the thumbnail saved on the filesystem and pass it on in the frontend.

For example : Image is saved at

/home/username/datadir/personid.png

I would like to give the url something like :

domainname.com/personid.png

Please note PersonId is unique, so I can use that constraint. The part to save the image is complete, I just don't know how to create an image URL out of the file saved on File-System.

Any help would be nice. Thanks a lot.. :-)

We are Borg
  • 5,117
  • 17
  • 102
  • 225

2 Answers2

5

You should intermediate this call with a request handler, something like

@RequestMapping("/image/{personId}")
@ResponseBody
public HttpEntity<byte[]> getPhoto(@PathVariable String personId) {
    byte[] image = org.apache.commons.io.FileUtils.readFileToByteArray(new File([YOUR PATH] + File.separator + personId + ".png"));

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_PNG); 
    headers.setContentLength(image.length);
    return new HttpEntity<byte[]>(image, headers);
}

Note also that you can use content analysis to determine the proper media type e.g. with the help of Apache Tika. This way you don't have to store filename, or hard-code the extension

Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • @Master Slave : This will send the array of bytes. I want to send the location of the file which is saved at the server so that it can be loaded accordingly. I don't want to send the file contents in the byte array but I want to send the location of the image as URL. For e.g. http://localhost:9090/assets/personId.png – TechGuy Dec 30 '15 at 04:46
1

First of all you can do it with two ways, first in your tomcat's conf folder there's a file named as server.xml, add below line into it

<Context docBase="YOUR/FILE/PATH"   path="/" />

After that if you save your files into YOUR/FILE/PATH tomcat will serve it to outside as domainname.com/filename

The second way is more complicated and you have to google it a bit. You can open a controller that serves your images to outside as Master Slave answered.

Rg.

Sercan Ozdemir
  • 4,641
  • 3
  • 34
  • 64
  • I tried your way, It is not working, can you assist me. I added on my tomcat, and restarted the server, I have pasted a file in movehere folder, but I get a 404. – We are Borg Apr 29 '15 at 13:27
  • Whenever you have time, can you please tell me what I have to do. Thanks. – We are Borg Apr 29 '15 at 14:58
  • go to your server (with a ssh connection maybe) after that go into your tomcat's folder, after that there's a folder named conf, in it **server.xml**. You have to right the line into it. Here's a stackoverflow question that can give you a clue: http://stackoverflow.com/questions/417658/how-to-config-tomcat-to-serve-images-from-an-external-folder-outside-webapps – Sercan Ozdemir Apr 30 '15 at 05:26
  • I am testing it on localhost, I know server.xml, like I said, I had added it, but I had no idea something needs to be added in the web.xml of my project too as mentioned in the link you gave. Can you edit your answer to include details for that? Thanks. – We are Borg Apr 30 '15 at 07:02
  • If you are using spring web mvc, there should not be any needs for this purpose. My answer is enough, since i applied this way by myself. – Sercan Ozdemir Apr 30 '15 at 10:10