-
-
Save jpukg/ea2b51057019cee5cb8288adcdff27c5 to your computer and use it in GitHub Desktop.
Serve Spring MVC image on response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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