首页 > 编程语言 > 详细

springboot通过流读图片并展示在浏览器

时间:2021-08-18 10:24:17      阅读:16      评论:0      收藏:0      [点我收藏+]
@GetMapping("/photo")
public void photo(HttpServletResponse response) throws IOException{
  ServletOutputStream outputStream = null;
   InputStream inputStream = null;

    try {
        String imgPath = "";
        if(StringUtils.isBlank(imgPath))
        {
            ClassPathResource classPathResource = new ClassPathResource("/static/image/demo.png");
            inputStream = classPathResource.getInputStream();
        }else{
            inputStream = FileUtil.getInputStream(imgPath);
        }
        response.setContentType("image/png");
        outputStream = response.getOutputStream();

        int len = 0;
        byte[] buffer = new byte[4096];
        while ((len = inputStream.read(buffer)) != -1)
        {
            outputStream.write(buffer, 0, len);
        }
        outputStream.flush();
    } catch (Exception e)
    {
        e.printStackTrace();
    } finally {
        outputStream.close();
        inputStream.close();
    }

}

springboot通过流读图片并展示在浏览器

原文:https://www.cnblogs.com/zhang-zhao/p/15155182.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!