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

}
上一篇:base64 InputStream互转


下一篇:单例模式的饿汉式读取db.properties文件中的内容