Servlet实现图片读取显示

1.导入jar包:commons-io-1.4.jar

2.index.jsp:

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9     <head>
10         <base href="<%=basePath%>">
11 
12         <title>文件上传</title>
13         <meta http-equiv="pragma" content="no-cache">
14         <meta http-equiv="cache-control" content="no-cache">
15         <meta http-equiv="expires" content="0">
16         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17         <meta http-equiv="description" content="This is my page">
18         <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21     </head>
22 
23     <body>
24             <div><a href="${pageContext.request.contextPath}/showPic.jsp">查看图片</a></div>
25     </body>
26 </html>

3.showPic.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9     <head>
10         <base href="<%=basePath%>">
11 
12         <title>文件上传</title>
13         <meta http-equiv="pragma" content="no-cache">
14         <meta http-equiv="cache-control" content="no-cache">
15         <meta http-equiv="expires" content="0">
16         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17         <meta http-equiv="description" content="This is my page">
18         <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21     </head>
22 
23     <body>
24         <div>
25             <img alt="图片" src="${pageContext.request.contextPath}/ShowPictureServlet?fileName=fanfan.jpg">
26         </div>
27     </body>
28 </html>

4.ShowPictureServlet.java

 pacgake com.pearl.util;

1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.IOException; 4 import java.io.OutputStream; 5 6 import javax.servlet.ServletConfig; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 public class ShowPictureServlet extends HttpServlet { 13 14 public void destroy() { 15 super.destroy(); 16 } 17 18 public void doGet(HttpServletRequest request, HttpServletResponse response) 19 throws ServletException, IOException { 20 //文件路径 21 String picFolder = "E:/upload/"; 22 String fileName = request.getParameter("fileName"); 23 if(fileName!=null && !fileName.equals("")){ 24 String mimeType = "image/gif"; 25 //设置content类型 26 response.setContentType(mimeType); 27 //设置大小 28 File file = new File(picFolder + fileName); 29 response.setContentLength((int) file.length()); 30 //打开文件并输出 31 FileInputStream inputStream = new FileInputStream(file); 32 OutputStream out = response.getOutputStream(); 33 34 //把文件复制到输出流 35 byte[] data = new byte[1024]; 36 int count = 0; 37 while ((count=inputStream.read(data))>=0){ 38 out.write(data, 0, count); 39 } 40 inputStream.close(); 41 out.close(); 42 } 43 } 44 45 public void doPost(HttpServletRequest request, HttpServletResponse response) 46 throws ServletException, IOException { 47 doGet(request, response); 48 } 49 50 51 public void init(ServletConfig config) throws ServletException { 52 super.init(config); 53 } 54 55 }

 5.web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <servlet>
 8     <description>This is the description of my J2EE component</description>
 9     <display-name>This is the display name of my J2EE component</display-name>
10     <servlet-name>ShowPictureServlet</servlet-name>
11     <servlet-class>com.pearl.util.ShowPictureServlet</servlet-class>
12   </servlet>
13   
14   <servlet-mapping>
15     <servlet-name>ShowPictureServlet</servlet-name>
16     <url-pattern>/ShowPictureServlet</url-pattern>
17   </servlet-mapping>
18  
19   <welcome-file-list>
20     <welcome-file>index.jsp</welcome-file>
21   </welcome-file-list>
22 </web-app>

6.完成。

原文地址:https://www.cnblogs.com/yeqrblog/p/4894323.html