1
votes

I have base64-encoded images (e.g. gif). I want to serve them via Spring-Boot web without base64-decoding them on server-side. The client should do the base64-decoding. It is clear that browsers can do that. Have a short look at https://jsfiddle.net/casiano/xadvz/. With the base64-encoded image inlined in the src of the img-tag it works fine. Now assume I have the base64-encoded image from that example in a file on the server (myfile_gif.txt with content "R0lG ... hAAOw==").

I want to serve that file myfile_gif.txt via Spring-Boot web, without decoding the base64 to binary on server-side. It should work in a way so that the following html snippet will actually show the image

<img src="http://localhost:8080/rest/testimage" />

Currently I have the following

@RestController
@RequestMapping("/rest")
public class RestController {
    @RequestMapping(path = "/testimage", method = RequestMethod.GET)
    @ResponseBody
    public HttpEntity<?> getTestImage() {
        InputStream stream = null;
        try {
            stream = new FileInputStream(new File("myfile_gif.txt"));
        } catch (FileNotFoundException e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }

        if (stream != null) {
            return ResponseEntity.ok()
            .contentType(MediaType.parseMediaType("image/gif;base64"))
            .header(HttpHeaders.CONTENT_ENCODING, "base64")
            .header(HttpHeaders.TRANSFER_ENCODING, "base64")
            .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"my.gif\"")
            .body(new InputStreamResource(stream));
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
    }
}

But it does not work - the image does not show in the browser. I can see in Developer-console in browser that the request to http://localhost:8080/rest/testimage gets response-code 200 and it says 2,54kb transfered, so it seems to be served alright. Selected response-headers

  • Content-Encoding: base64
  • Transfer-Encoding: base64, chunked
  • Content-Type: image/gif;charset=UTF-8

It have tried all kinds of things that I could think of, but cannot make it work. Can you help me make it work?

1
Here is myfile_git.txt: gist.github.com/steff1193/… - Per Steffensen

1 Answers

1
votes

option 1:

String base64 = "your file"; // get base-64 encoded string
    byte[] bytes = Base64.decodeBase64(base64);
    try (InputStream inputStream = new ByteArrayInputStream(bytes)) {
        StreamUtils.copy(inputStream, response.getOutputStream());
        response.setContentType(MediaType.IMAGE_PNG_VALUE);
    } catch (IOException e) {
        // handle
    }
    return new ResponseEntity(HttpStatus.OK);

OPTION 2:

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

   // 1. download img your location... 
    byte[] image = ... 

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

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