-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidateCodeServlet.java
More file actions
102 lines (87 loc) · 2.87 KB
/
ValidateCodeServlet.java
File metadata and controls
102 lines (87 loc) · 2.87 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.itecheasy.ph3.web;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.xwork.StringUtils;
import com.itecheasy.ph3.web.utils.SessionUtils;
public class ValidateCodeServlet extends HttpServlet {
private static final long serialVersionUID = 3889954231354L;
// 验证码图片的宽度
private int width;
// 验证码图片的高度
private int height;
// 验证码字符个数
private int codeCount;
private int x = 0;
// 字体高度
private int fontHeight;
private int codeY;
char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K','M', 'N', 'P','R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z','2', '3', '4', '5', '6', '7', '8', '9' };
/**
* 初始化验证图片属性
*/
@Override
public void init() throws ServletException {
initData(69,28);
}
public void initData(int w, int h){
width =w;
height =h;
codeCount = 4;
x = width / (codeCount + 1);
fontHeight = height - 2;
codeY = height - 4;
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
java.io.IOException {
String w = req.getParameter("w");
String h = req.getParameter("h");
if(StringUtils.isNotEmpty(w) && StringUtils.isNotEmpty(h)){
initData(Integer.parseInt(w),Integer.parseInt(h));
}
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
Font font = new Font("Arial", Font.PLAIN, fontHeight);
g.setFont(font);
g.setColor(Color.BLACK);
Random random = new Random();
for (int i = 0; i < 3; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
for (int i = 0; i < codeCount; i++) {
String strRand = String.valueOf(codeSequence[random.nextInt(30)]);
red = random.nextInt(200);
green = random.nextInt(180);
blue = random.nextInt(255);
g.setColor(new Color(red, green, blue));
g.drawString(strRand,i*x+x/2, codeY);
randomCode.append(strRand);
}
SessionUtils.setVerifyCode(req, randomCode.toString());
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}
}