Java生成验证码,保存到指定目录

前段时间,帮一个朋友弄个登录界面,用到了java生成验证码。
config_verifyCode.xml是配置保存目录。如需改动保存目录,直接改xml配置即可。

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class RandCodeImage extends HttpServlet
{

public RandCodeImage()
{
lineWidth = 2;
width = 90;
height = 32;
count = 200;
randomCode = null;
x = 0;
codeNum = 4;
}

public void init()
throws ServletException
{
String strCodeNums = getInitParameter("codeCount");
String strW = getInitParameter("width");
String strH = getInitParameter("height");
try
{
if(strH != null && strH.length() != 0)
height = Integer.parseInt(strH);
if(strW != null && strW.length() != 0)
width = Integer.parseInt(strW);
if(strCodeNums != null && strCodeNums.length() != 0)
codeNum = Integer.parseInt(strCodeNums);
}
catch(NumberFormatException numberformatexception) { }
x = width / (codeNum + 1);
System.out.println("xx = " + x);
fontHeight = height - 2;
codeY = height - 4;
}

private Color getRandColor(int fc, int bc)
{
Random random = new Random();
if(fc > 255)
fc = 255;
if(bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}

public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String sessionName = request.getParameter("sessionName");
if(sessionName == null || sessionName.length() == 0)
sessionName = "rand";
this.x = width / (codeNum + 1);
fontHeight = height - 4;
codeY = height - 4;

response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0L);

BufferedImage image = new BufferedImage(width, height, 1);
Graphics2D g = (Graphics2D)image.getGraphics();
Random random = new Random();

g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
fontHeight = height - 2;
Font font = new Font("Fixedsys", 0, fontHeight);

g.setFont(font);
g.setColor(getRandColor(0, 20));
g.drawRect(0, 0, width - 1, height - 1);

g.setColor(getRandColor(200, 250));
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);

g.setColor(getRandColor(160, 200));
for(int j = 0; j < 20; j++)
{
int x = random.nextInt(width - lineWidth - 1) + 1;
int y = random.nextInt(height - lineWidth - 1) + 1;
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}

String sRand = "";
int R = 0;
int G = 0;
int B = 0;
for(int i = 0; i < 4; i++)
{
String rand = String.valueOf(codes[random.nextInt(32)]);
sRand = sRand + rand;
R = random.nextInt(255);
G = random.nextInt(255);
B = random.nextInt(255);
g.setColor(new Color(R, G, B));
g.drawString(rand, (i + 1) * this.x, codeY);
}
g.dispose();
long lasting = System.currentTimeMillis();
String yzmpath = null;
try{
File f = new File("config_verifyCode.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("VALUE");
for (int i=0;i<nl.getLength();i++){
yzmpath = doc.getElementsByTagName("Dir").item(i).getFirstChild().getNodeValue();
}
} catch(Exception e){
e.printStackTrace();
}
ImageIO.write(image, "JPEG", new File(yzmpath + sRand.toString() + ".jpg"));
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet Out VerifyCode</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" <t0>" + sRand.toString() + "</t0>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

private int lineWidth;
private int width;
private int height;
private int count;
StringBuffer randomCode;
char codes[] = {
'2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z'
};
private int x;
private int fontHeight;
private int codeY;
private int codeNum;
}