0

I'm using spring mvc and want to create a method which must return binary file. I've found some tutorials but nothing works for me. This is my part of my code:

@RequestMapping(value = "/get-frame/{fileId}", method = RequestMethod.GET)
public void getVideoFrame(@PathVariable("fileId") String filename,
                          HttpServletResponse response) throws IOException {
    ...
    ...
    ...
    byte[] image = ...
    OutputStream outputStream = response.getOutputStream();
    outputStream.write(image);
    outputStream.close();
    response.flushBuffer();
}

This method always returns error 406. What's wrong?

Thanks

Igor Belykh
  • 101
  • 5
  • 13

1 Answers1

1

You should set the header, content type and length of the response.

try (OutputStream out = response.getOutputStream()) {
    byte[] image = ...
    response.setContentLength(image.length);
    response.setContentType("image/png"); //or something more generic...
    response.setHeader("Accept-Ranges", "bytes");
    response.setStatus(HttpServletResponse.SC_OK);

    out.write(image);
}