标签:style blog http color java os io strong 文件
需求:
通过servlet读取图片地址,然后显示在jsp页面上
jsp页面:
1 <img style="width:240px; height:200px;" src="<%=request.getContextPath()%>/chartServlet">
wel.xml 配置:
1 <servlet> 2 <servlet-name>chartServlet</servlet-name> 3 <servlet-class>com.stronginfo.transenly.website.servlet.chartServlet</servlet-class> 4 </servlet> 5 <servlet-mapping> 6 <servlet-name>chartServlet</servlet-name> 7 <url-pattern>/chartServlet</url-pattern> 8 </servlet-mapping>
java代码:
1 package com.stronginfo.transenly.website.servlet; 2 3 import java.io.FileInputStream; 4 import java.io.IOException; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.ServletOutputStream; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import com.stronginfo.core.dao.OrderDao; 13 import com.stronginfo.transenly.util.ConfigManager; 14 15 public class chartServlet extends HttpServlet 16 { 17 18 private static final long serialVersionUID = 1L; 19 20 21 22 23 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException 24 { 25 26 27 ServletOutputStream outStream = res.getOutputStream();// 得到向客户端输出二进制数据的对象 28 FileInputStream fis = new FileInputStream(图片路径); // 以byte流的方式打开文件 29 // 读数据 30 byte data[] = new byte[1000]; 31 while (fis.read(data) > 0) 32 { 33 outStream.write(data); 34 } 35 fis.close(); 36 res.setContentType("image/*"); // 设置返回的文件类型 37 outStream.write(data); // 输出数据 38 39 outStream.close(); 40 41 } 42 43 }
标签:style blog http color java os io strong 文件
原文地址:http://www.cnblogs.com/na-java/p/3929323.html