Skip to content

Instantly share code, notes, and snippets.

@jpukg
Forked from marcoberri/findImageApi.java
Created October 10, 2016 18:47
Show Gist options
  • Select an option

  • Save jpukg/ea2b51057019cee5cb8288adcdff27c5 to your computer and use it in GitHub Desktop.

Select an option

Save jpukg/ea2b51057019cee5cb8288adcdff27c5 to your computer and use it in GitHub Desktop.
Serve Spring MVC image on response
@RequestMapping(value = "/image/{id}", method = RequestMethod.GET)
public void findImage(@PathVariable("id") String id, HttpServletResponse resp){
final Foto anafoto = <find object>
resp.reset();
resp.setContentType(MediaType.IMAGE_JPEG_VALUE);
resp.setContentLength(anafoto.getImage().length);
final BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(anafoto.getImageInBytes()));
try {
FileCopyUtils.copy(in, resp.getOutputStream());
resp.flushBuffer();
} catch (final IOException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment