8

I have a Spring Boot application which should act like a proxy.

It should process requests like "http://imageservice/picture/123456"

Then the application should generate a new request to "http://internal-picture-db/123456.jpg" where it should download the picture behind it (123456.jpg) and then pass it to the response and serve it.

It should be like...

@RequestMapping("/picture/{id}")
public String getArticleImage(@PathVariable String id, HttpServletResponse response) {

    logger.info("Requested picture >> " + id + " <<");

    // 1. download img from http://internal-picture-db/id.jpg ... 

    // 2. send img to response... ?!

    response.???

}

I hope it's clear what I mean...

So my question is: What is the best way to do so?

And just for information it is not possible to just send a redirect because the system is not available in the internet.

DaUser
  • 357
  • 3
  • 5
  • 19

2 Answers2

10

I would use the response body to return the image and not a view for example:

@RequestMapping("/picture/{id}")
@ResponseBody
public HttpEntity<byte[]> getArticleImage(@PathVariable String id) {

    logger.info("Requested picture >> " + id + " <<");

    // 1. download img from http://internal-picture-db/id.jpg ... 
    byte[] image = ...

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_JPEG);
    headers.setContentLength(image.length);

    return new HttpEntity<byte[]>(image, headers);
}

You have a post which can help you to download the image from an other url: how to download image from any web page in java

Community
  • 1
  • 1
Steph
  • 779
  • 1
  • 8
  • 18
  • Works but at a closer look the problem is that the method is called twice. Just because of the HttpEntity. If I changed the return type to String and just return a random String with also downloading the image, its only called once. So right now the code in this method get executed twice. Any ideas or workarounds? – DaUser Apr 23 '15 at 14:20
  • what happen if you return a String and encode the image to base64 with Base64.encodeBase64URLSafeString(byte[] binaryData) ? – Steph Apr 23 '15 at 14:45
  • It only displays the String then, because there is no content type specified. I'm not sure how to handle it because the image I will download and serve is not always jpg , could be a gif too. I need to set the content type dynamic. – DaUser Apr 24 '15 at 08:20
  • I tried `@RequestMapping(value = "/picture2/{id}", headers = "Accept=image/jpeg, image/jpg, image/png, image/gif") public @ResponseBody byte[] getArticleImage2(@PathVariable String id) {` which works but also get executed twice... ?! It seems that the code for downloading let this execute twice... ?! – DaUser Apr 24 '15 at 08:59
  • [Related problem](http://stackoverflow.com/questions/29843465/java-spring-boot-restcontroller-requestmapping-executed-twice) – DaUser Apr 24 '15 at 09:18
0
@RequestMapping("/picture/{id}")
public ResponseEntity<byte[]> getArticleImage(@PathVariable String id) {

    logger.info("Requested picture >> " + id + " <<");

    // 1. download img from http://internal-picture-db/id.jpg ... 
    byte[] image = ...

    return new ResponseEntity<byte[]>(image, HttpStatus.OK);
}

And see the code for downloading the image in this post.

azhar
  • 167
  • 11