本文是博主原创,转载请注明出处。
1.核心Servlet代码
package com.rc.testImg;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author CharlesRan
*
* @projectName TestImg
*
* @City ChongQing
*
* @packegeName TestImg
*
* @Date 2017年10月14日 下午9:16:45
*
*/
public class showImgServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("开始请求后台!!!!");
File file=new File("C:/Users/Administrator/Desktop/123/2.png");
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos=new BufferedOutputStream(response.getOutputStream());
response.setHeader("Content-Type","image/jpeg");
byte b[]=new byte[1024];
int read;
try {
while((read=bis.read(b))!=-1){
bos.write(b, 0, read);
}
request.getRequestDispatcher("message.jsp").forward(request, response);
} catch (Exception e) {
// TODO: handle exception
} finally{
if(bos!=null){
bos.close();
}
if(bis!=null){
bis.close();
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
}
2.Web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>TestImg</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>showImgServlet</servlet-name>
<servlet-class>com.rc.testImg.showImgServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>showImgServlet</servlet-name>
<url-pattern>/showImgServlet</url-pattern>
</servlet-mapping>
</web-app>
3.启动首页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查看图片</title>
</head>
<script type="text/javascript">
function seePic() {
document.getElementById("seePicLink").click();
}
</script>
<body>
<h1>查看图片</h1>
<a href="imgInfo.jsp" style="display: none;" id="seePicLink"></a>
<button οnclick="seePic()">查看图片</button>
</body>
</html>
4.负责显示图片的jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>通过流的形式加载图片到JSP页面显示</title>
</head>
<body>
<h1>通过流的形式加载图片到JSP页面显示</h1>
<img alt="" src="http://localhost:8080/TestImg/showImgServlet" title="" style="width: 300px;height: 300px">
</body>
</html>