前言:

本文内容:Response下载文件、Response验证码实现

推荐免费JavaWeb入门到实战视频:【狂神说Java】JavaWeb入门到实战_哔哩哔哩_bilibili

Response下载文件

Web服务器接收到客户端的http请求,针对这个请求,分别创建一个代表请求的HttpServletRequest对象,代表响应的HttpServletResponse

  • 如果要获取客户端请求的参数:HttpServletRequest
  • 如果要为客户端响应数据HttpServletResponse

简单分类

负责向浏览器发送数据的方法

1
2
public ServletOutputStream getOutputStream() throws IOException;
public PrintWriter getWriter() throws IOException;

负责向浏览器发送响应头的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void setCharacterEncoding(String charset);

public void setContentLength(int len);

public void setContentLengthLong(long len);

public void setContentType(String type);

public void setDateHeader(String name, long date);

public void addDateHeader(String name, long date);

public void setHeader(String name, String value);

public void addHeader(String name, String value);

public void setIntHeader(String name, int value);

public void addIntHeader(String name, int value);

常见应用

像浏览器输出消息

下载文件

  • 获取下载文件的路径
  • 下载文件名
  • 设置浏览器以支持下载我们需要的东西
  • 获取下载文件的输入流
  • 创建缓冲区
  • 获取OutputStream对象
  • 将FileOutputStream流写入到Buffer缓冲区
  • 使用OutputStream将缓冲区中的数据输出到客户端

下载文件例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.jokerdig.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

/**
* @author Joker大雄
* @data 2022/3/30 - 12:43
**/
public class FileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取下载文件的路径
String realPath = "D:\\Project\\project3\\JavaWeb\\javaweb-02-servlet\\response\\target\\classes\\1.png";
System.out.println("下载文件的路径"+realPath);
// 下载文件名
String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
// 设置浏览器以支持下载我们需要的东西
resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"utf-8"));
// 获取下载文件的输入流
FileInputStream input = new FileInputStream(realPath);
// 创建缓冲区
int len=0;
byte[] buffer = new byte[1024];
// 获取OutputStream对象
ServletOutputStream out = resp.getOutputStream();
// 将FileOutputStream流写入到Buffer缓冲区
while((len=input.read(buffer))>0){
out.write(buffer,0,len);
}
// 使用OutputStream将缓冲区中的数据输出到客户端
input.close();
out.close();

}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}

web.xml配置文件

1
2
3
4
5
6
7
8
<servlet>
<servlet-name>down</servlet-name>
<servlet-class>com.jokerdig.servlet.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>down</servlet-name>
<url-pattern>/down</url-pattern>
</servlet-mapping>

Response验证码实现

验证码如何实现:

  • 前端实现
  • 后端实现,需要Java图片类,生成一个图片

后端实现随机验证码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.jokerdig.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
* @author Joker大雄
* @data 2022/3/30 - 13:16
**/
public class ImageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 如何让浏览器3s刷新一次
resp.setHeader("refresh","3");
// 在内存中创建图片
BufferedImage image = new BufferedImage(50, 20, BufferedImage.TYPE_3BYTE_BGR);
// 得到图片
Graphics gr=(Graphics2D)image.getGraphics();// 取得画笔
// 设置图片背景颜色
gr.setColor(Color.WHITE);
gr.fillRect(0,0,50,20);
// 给图写数据
gr.setColor(Color.BLUE);
gr.setFont(new Font(null,Font.BOLD,20));
gr.drawString(makeRandom(),0,20);
// 告诉浏览器,这个请求用图片的方式打开
resp.setContentType("image/jpg");
// 浏览器的缓存,我们需要去掉
resp.setDateHeader("expires",-1);
resp.setHeader("Cache-Control","no-cache");
resp.setHeader("Pragma","no-cache");
// 把图片写给浏览器
ImageIO.write(image,"jpg",resp.getOutputStream());
}
// 生成随机数
private String makeRandom(){
Random random = new Random();
String num = random.nextInt(9999) + "";
StringBuffer stringBF = new StringBuffer();
for(int i=0;i<4-num.length();i++){
stringBF.append("0");
}
String back = stringBF.toString()+num;
return back;
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}

web.xml注册

1
2
3
4
5
6
7
8
<servlet>
<servlet-name>image</servlet-name>
<servlet-class>com.jokerdig.servlet.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>image</servlet-name>
<url-pattern>/image</url-pattern>
</servlet-mapping>