I have BufferedImage in my spring boot application. Now I want to send that file to user. How can I do that ?
I'm looking for method to convert BufferedImage into ResponseEntity.
I have BufferedImage in my spring boot application. Now I want to send that file to user. How can I do that ?
I'm looking for method to convert BufferedImage into ResponseEntity.
you can also convert it to byte[]
using javax.imageio.ImageIO
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage , "png", byteArrayOutputStream);
byte[] imageInByte = baos.toByteArray();
then you controller is simplified:
@RequestMapping(value = "/path", method = GET)
public ResponseEntity<byte[]> getResource() {
return ResponseEntity.status(HttpStatus.OK)
.header(HttpHeaders.CONTENT_DISPOSITION, "filename=\"image.png"\")
.contentType(MediaType.IMAGE_PNG)
.body(imageInByte);