springboot返回图片流的实现示例
更新时间:2022年08月15日 11:02:19 作者:卑微小钟
本文主要介绍了springboot返回图片流的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
一、返回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.jpg"))
}
二、返回byte[]
返回byte[]可以不用配置图片转换器,而自接被识别
@GetMapping(value = "/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();
}
到此这篇关于springboot返回图片流的实现示例的文章就介绍到这了,更多相关springboot返回图片流内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
启动springboot项目时报错:无法访问org.springframework.web.bind.annotatio
这篇文章给大家分享了启动springboot项目时报错:无法访问org.springframework.web.bind.annotation.GetMapping …具有错误的版本 61.0,应为52.0的解决方案,文中通过图文介绍的非常详细,需要的朋友可以参考下2023-10-10
java Swing实现选项卡功能(JTabbedPane)实例代码
这篇文章主要介绍了java Swing实现选项卡功能(JTabbedPane)实例代码的相关资料,学习java 基础的朋友可以参考下这个简单示例,需要的朋友可以参考下2016-11-11
利用openoffice+jodconverter-code-3.0-bate4实现ppt转图片
这篇文章主要为大家详细介绍了利用openoffice+jodconverter-code-3.0-bate4实现ppt转图片,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2019-07-07


最新评论