实现机理:生成一串随机数,其中可以是固定值,可以是可变值,本人的随机数长度固定为4(可根据您自己的程序进行改变),等到随机字符串产生后,将此字符串打乱成字符,然后一个一个的把字符画到输出流里,其中每个字符的颜色即RGB值也是随机产生,在画面的同时,再产生若干条干扰线,以防止验证码被破解。然后把图象流输出到客户端。其中客户端的HTML代码利用img标签实现。
1package com.kai;
2
3import java.awt.Color;
4import java.awt.Font;
5import java.awt.Graphics2D;
6import java.awt.image.BufferedImage;
7import java.io.IOException;
8import java.util.Random;
9
10import javax.imageio.ImageIO;
11import javax.servlet.ServletException;
12import javax.servlet.http.HttpServlet;
13import javax.servlet.http.HttpServletRequest;
14import javax.servlet.http.HttpServletResponse;
15
16public class Getshowimg extends HttpServlet {
17
18 private static final long serialVersionUID = -4880176221277149304L;
19
20 public void service(HttpServletRequest req, HttpServletResponse resp)
21 throws ServletException, IOException {
22 //设置响应格式
23 resp.setContentType("image/jpeg");
24 //定义一个BufferedImage对象 其构造函数属性参照API
25 BufferedImage image = new BufferedImage(80,20,BufferedImage.SCALE_SMOOTH);
26 //根据上述对象产生一个Graphics2D上下文
27 Graphics2D graphics = image.createGraphics();
28 //将上下文颜色设置为白色
29 graphics.setColor(Color.WHITE);
30 graphics.fillRect(0, 0,100,20);
31 //设置书写字体
32 graphics.setFont(new Font("Roman",Font.CENTER_BASELINE,18));
33 //randomString方法见后面,负责产生由4个字符组成的字符串
34 String randomStr=randomString(4);
35 for(int i=0;i<4;i++){
36 //随机设置上下文颜色
37 graphics.setColor(new Color(new Random().nextInt(250),new Random().nextInt(250),
38 new Random().nextInt(250)));
39 //按字符把其画出来
40 graphics.drawString(randomStr.substring(i,i+1),15*i+15,15);
41 //产生干扰线
42 Random ran = new Random();
43 graphics.drawLine(ran.nextInt(80),ran.nextInt(20),ran.nextInt(80), ran.nextInt(20));
44 }
45 //禁止缓冲代码,没有这段的话则不能进行更新显示,删除效果请自行测试
46 resp.setHeader("Prama", "no-cache");
47 resp.setHeader("Coche-Control", "no-cache");
48 resp.setDateHeader("Expires", 0);
49 //写入到客户端
50 ImageIO.write(image,"jpeg",resp.getOutputStream());
51 }
52
53 //number 产生有number个字符组成的字符串
54 public String randomString(int number){
55 //字符串的内容组成
56 String str="1234567890qwertyuioplkjhgfdsazxcvbnm";
57 char[] chars=str.toCharArray();
58 int length=chars.length;
59 StringBuilder sb=new StringBuilder();
60 Random random=new Random();
61 //随机生成number个字符
62 for(int i=0;i<number;i++){
63 //把单个字符转换成字符串然后追加到已经产生的字符串中
64 sb.append(new String(new Character(chars[random.nextInt(length)]).toString()));
65 }
66 return sb.toString();
67 }
68}
69
2
3import java.awt.Color;
4import java.awt.Font;
5import java.awt.Graphics2D;
6import java.awt.image.BufferedImage;
7import java.io.IOException;
8import java.util.Random;
9
10import javax.imageio.ImageIO;
11import javax.servlet.ServletException;
12import javax.servlet.http.HttpServlet;
13import javax.servlet.http.HttpServletRequest;
14import javax.servlet.http.HttpServletResponse;
15
16public class Getshowimg extends HttpServlet {
17
18 private static final long serialVersionUID = -4880176221277149304L;
19
20 public void service(HttpServletRequest req, HttpServletResponse resp)
21 throws ServletException, IOException {
22 //设置响应格式
23 resp.setContentType("image/jpeg");
24 //定义一个BufferedImage对象 其构造函数属性参照API
25 BufferedImage image = new BufferedImage(80,20,BufferedImage.SCALE_SMOOTH);
26 //根据上述对象产生一个Graphics2D上下文
27 Graphics2D graphics = image.createGraphics();
28 //将上下文颜色设置为白色
29 graphics.setColor(Color.WHITE);
30 graphics.fillRect(0, 0,100,20);
31 //设置书写字体
32 graphics.setFont(new Font("Roman",Font.CENTER_BASELINE,18));
33 //randomString方法见后面,负责产生由4个字符组成的字符串
34 String randomStr=randomString(4);
35 for(int i=0;i<4;i++){
36 //随机设置上下文颜色
37 graphics.setColor(new Color(new Random().nextInt(250),new Random().nextInt(250),
38 new Random().nextInt(250)));
39 //按字符把其画出来
40 graphics.drawString(randomStr.substring(i,i+1),15*i+15,15);
41 //产生干扰线
42 Random ran = new Random();
43 graphics.drawLine(ran.nextInt(80),ran.nextInt(20),ran.nextInt(80), ran.nextInt(20));
44 }
45 //禁止缓冲代码,没有这段的话则不能进行更新显示,删除效果请自行测试
46 resp.setHeader("Prama", "no-cache");
47 resp.setHeader("Coche-Control", "no-cache");
48 resp.setDateHeader("Expires", 0);
49 //写入到客户端
50 ImageIO.write(image,"jpeg",resp.getOutputStream());
51 }
52
53 //number 产生有number个字符组成的字符串
54 public String randomString(int number){
55 //字符串的内容组成
56 String str="1234567890qwertyuioplkjhgfdsazxcvbnm";
57 char[] chars=str.toCharArray();
58 int length=chars.length;
59 StringBuilder sb=new StringBuilder();
60 Random random=new Random();
61 //随机生成number个字符
62 for(int i=0;i<number;i++){
63 //把单个字符转换成字符串然后追加到已经产生的字符串中
64 sb.append(new String(new Character(chars[random.nextInt(length)]).toString()));
65 }
66 return sb.toString();
67 }
68}
69
<script type="text/javascript">
function doinit()
{
document.getElementById("img0").src="servlet/Getshowimg";
}
</script>
</head>
<body>
验证码:
<IMG src='servlet/Getshowimg' id="img0" vspace=3 ><a onclick="doinit()" style="cursor: pointer">看不清?</a>
</body>
function doinit()
{
document.getElementById("img0").src="servlet/Getshowimg";
}
</script>
</head>
<body>
验证码:
<IMG src='servlet/Getshowimg' id="img0" vspace=3 ><a onclick="doinit()" style="cursor: pointer">看不清?</a>
</body>