[转] img的src通过请求的方式来显示图片

来源:互联网 发布:春城影院软件 编辑:程序博客网 时间:2024/06/03 21:33

原文链接:http://blog.csdn.net/kouwoo/article/details/50015693


1.jsp

[html] view plain copy
 print?
  1. <img alt="test" src="getImg2.do">  


2.controller

[java] view plain copy
 print?
  1. @RequestMapping("getImg2")  
  2. public void getImg2(HttpServletRequest request, HttpServletResponse response)  
  3.         throws IOException {  
  4.     FileInputStream fis = null;  
  5.     OutputStream os = null;  
  6.     try {  
  7.         fis = new FileInputStream("d:/log.png");  
  8.         os = response.getOutputStream();  
  9.         int count = 0;  
  10.         byte[] buffer = new byte[1024 * 8];  
  11.         while ((count = fis.read(buffer)) != -1) {  
  12.             os.write(buffer, 0, count);  
  13.             os.flush();  
  14.         }  
  15.     } catch (Exception e) {  
  16.         e.printStackTrace();  
  17.     } finally {  
  18.         try {  
  19.             fis.close();  
  20.             os.close();  
  21.         } catch (IOException e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.     }  
  25. }  



阅读全文
0 0
原创粉丝点击