spring|spring boot 返回图片流

spring boot 返回图片流 一、返回BufferedImage

由于spring mvc不支持返回BufferedImage ,所以增加图片转换器
@Configuration public class WebMvcConfig implements WebMvcConfigurer { /** * 增加图片转换器 * @param converters */ @Override public void extendMessageConverters(List> converters) { converters.add(new BufferedImageHttpMessageConverter()); } }

@GetMapping(value = "https://www.it610.com/img",produces = MediaType.IMAGE_PNG_VALUE) public BufferedImage getImage() throws Exception { return ImageIO.read(new FileInputStream(new File("D:/test.jpg")) }

二、返回byte[]
【spring|spring boot 返回图片流】返回byte[]可以不用配置图片转换器,而自接被识别
@GetMapping(value = "https://www.it610.com/img",produces = MediaType.IMAGE_PNG_VALUE) public byte[] getImage() throws Exception { bufferedImage = ImageIO.read(new FileInputStream(new File("D:/test.jpg")) ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "png", out); return out.toByteArray(); }

    推荐阅读