servet页面
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.*;
@WebServlet("/image")
public class IoImageTestServlet extends HttpServlet {
@Override
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
File file = new File("D:/image/image.jpg");
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType("image/jpg");
byte[] b = new byte[1024];
int read = 0;
try {
while ((read = bufferedInputStream.read(b)) != -1) {
bufferedOutputStream.write(b, 0, read);
}
request.getRequestDispatcher("index.jsp").forward(request, response);
} catch (Exception e) {
} finally {
if (bufferedOutputStream != null) {
bufferedOutputStream.close();
}
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
}
}
@Override
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
doPost(request, response);
}
}
页面 有多种方式显示到页面
第一种 通过img标签接收
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<img src="/image" width="1200px" height="800px" >
</body>
</html>
第二种 修改web.xml文件中的的页面显示默认页面
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>image</welcome-file>
</welcome-file-list>
</web-app>
效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cb649ff1e0832e624985315e6f0b74a7.png#pic_center)