一、返回BufferedImage
二、返回byte[]
一、返回BufferedImage由于spring mvc不支持返回BufferedImage ,所以增加图片转换器
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
/**
* 增加图片转换器
* @param converters
*/
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new BufferedImageHttpMessageConverter());
}
}
@GetMapping(value = "/img",produces = MediaType.IMAGE_PNG_VALUE)
public BufferedImage getImage() throws Exception {
return ImageIO.read(new FileInputStream(new File("D:/test.webp"))
}
二、返回byte[]
返回byte[]可以不用配置图片转换器,而自接被识别
@GetMapping(value = "/img",produces = MediaType.IMAGE_PNG_VALUE)
public byte[] getImage() throws Exception {
bufferedImage = ImageIO.read(new FileInputStream(new File("D:/test.webp"))
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", out);
return out.toByteArray();
}
到此这篇关于springboot返回图片流的实现示例的文章就介绍到这了,更多相关springboot返回图片流内容请搜索易知道(ezd.cc)以前的文章或继续浏览下面的相关文章希望大家以后多多支持易知道(ezd.cc)!