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

本文展示了一个简单的Java Web应用程序示例,通过该程序可以将本地磁盘上的图片加载到网页上并显示出来。示例中使用了Servlet来处理HTTP请求,并通过FileInputStream读取图片文件,再利用HttpServletResponse.getOutputStream()方法将图片数据写入HTTP响应中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接:http://blog.youkuaiyun.com/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. }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值