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();
}
推荐阅读
- Java|Spring Boot Start之mqtt框架封装
- idea|spring boot 集成redis
- spring|Spring Boot的特点
- postman|postman工具的使用
- java|java mock verify_使用Mockito进行单元测试【1】——mock and verify[转]
- java|记一次Spring boot 和Vue前后端分离的入门培训
- 前端基础|众多mock工具,这一次我选对了
- ServiceLoader(Java 的 SPI 加载方案)
- spring|springboot+layui简单增删改查整合