@RequestMapping(value = "/image-manual-response", method = RequestMethod.GET) public void getImageAsByteArray(HttpServletResponse response) throws IOException { InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg"); response.setContentType(MediaType.IMAGE_JPEG_VALUE); IOUtils.copy(in, response.getOutputStream()); }
or
@GetMapping( value = "/get-image-with-media-type", produces = MediaType.IMAGE_JPEG_VALUE ) public @ResponseBody byte[] getImageWithMediaType() throws IOException { InputStream in = getClass() .getResourceAsStream("/com/baeldung/produceimage/image.jpg"); return IOUtils.toByteArray(in); }
References
https://www.baeldung.com/spring-mvc-image-media-data
https://www.baeldung.com/spring-controller-return-image-file