zoukankan      html  css  js  c++  java
  • springboot通过流读图片并展示在浏览器

    @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();
        }

    }
  • 相关阅读:
    3.1C#中的命名空间
    2章总结
    2.4冒泡排序
    2.3 C#中的数组
    2.2二重循环
    2.1c#中的循环语句
    1章总结
    docker内外数据拷贝
    搭建docker环境
    centos7 部署Apache的httpd服务器
  • 原文地址:https://www.cnblogs.com/zhang-zhao/p/15155182.html
Copyright © 2011-2022 走看看