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.. :-)

7
  • as 3rd alternative check stackoverflow.com/questions/8195213/… Commented Apr 29, 2015 at 13:13
  • Please be careful wrt the security issues w file uploads: owasp.org/index.php/Unrestricted_File_Upload Commented Apr 29, 2015 at 15:10
  • @NeilMcGuigan What are you talking about??? Commented Apr 29, 2015 at 15:13
  • I'm talking about the fact that there are many security issues related to user-uploaded files and that you should be aware of them. Read the article. Commented Apr 29, 2015 at 15:33
  • @NeilMcGuigan : What are you talking about, we are downloading files here, not uploading. Commented Apr 29, 2015 at 15:40

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

@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. localhost:9090/assets/personId.png
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.

5 Comments

I tried your way, It is not working, can you assist me. I added <Context docBase="/home/akshay/movehere/" path="/" /> on my tomcat, and restarted the server, I have pasted a file in movehere folder, but I get a 404.
Whenever you have time, can you please tell me what I have to do. Thanks.
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: stackoverflow.com/questions/417658/…
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.