JSP 之生成随即验证图片


 

 1  <%@ page contentType="image/jpeg;charset=gbk" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%>
 2     <%!
 3     
 4     /**
 5         使用方法:
 6         1.页面显示:在要使用验证码的页面中,加<img src="image.jsp">即可显示,其中image.jsp为本JSP的文件名
 7         2.后台使用:在登录校验代码之前(一般在控制器中),先从session中取出vcode的值(session.getAtrribute("vcode"))
 8             即为用户需要匹配的验证码
 9           然后从请求中取出用户页面输入的验证码 和刚从session中取出的值匹配 即可完成验证码校验  
10     */
11     Color getRandColor(int fc, int bc) {//给定范围获得随机颜色
12         Random random = new Random();
13         if (fc > 255)
14             fc = 255;
15         if (bc > 255)
16             bc = 255;
17         int r = fc + random.nextInt(bc - fc);
18         int g = fc + random.nextInt(bc - fc);
19         int b = fc + random.nextInt(bc - fc);
20         return new Color(r, g, b);
21     }%>
22     <%
23         //设置页面不缓存
24         response.setHeader("Pragma", "no-cache");
25         response.setHeader("Cache-Control", "no-cache");
26         response.setDateHeader("Expires", 0);
27 
28         // 在内存中创建图象
29         int width = 60, height = 20;
30         BufferedImage image = new BufferedImage(width, height,
31                 BufferedImage.TYPE_INT_RGB);
32 
33         // 获取图形上下文
34         Graphics g = image.getGraphics();
35 
36         //生成随机类
37         Random random = new Random();
38 
39         // 设定背景色
40         g.setColor(getRandColor(200, 250));
41         g.fillRect(0, 0, width, height);
42 
43         //设定字体
44         g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
45 
46         // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
47         g.setColor(getRandColor(160, 200));
48         for (int i = 0; i < 155; i++) {
49             int x = random.nextInt(width);
50             int y = random.nextInt(height);
51             int xl = random.nextInt(12);
52             int yl = random.nextInt(12);
53             g.drawLine(x, y, x + xl, y + yl);
54         }
55 
56         // 取随机产生的认证码(4位数字)
57         String sRand = "";
58         char w = 0;
59         String words = "AaBbCcDd0EeFfGg1HhIiJjKkLlMm3NnOoPpQ7qRrSsTtUuVvWwXxYyZz";
60         for (int i = 0; i < 4; i++) {
61             for (int j = 0; j < words.length(); j++) {
62                 w = words.charAt(random.nextInt(words.length() - 1));
63             }
64             //产生数字
65             String rand = "";
66             if (i % 2 == 0) {
67                 rand = String.valueOf(random.nextInt(10));
68             } else {
69                 rand = w + "";
70             }
71             //产生字母
72             sRand += rand;
73             // 将认证码显示到图象中
74             g.setColor(new Color(20 + random.nextInt(110), 20 + random
75                     .nextInt(110), 20 + random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
76             g.drawString(rand, 13 * i + 6, 16);
77         }
78 
79         // 将认证码存入SESSION
80         session.setAttribute("vcode", sRand);
81 
82         // 图象生效
83         g.dispose();
84         // 输出图象到页面
85         response.reset();
86         out.clear();
87         out = pageContext.pushBody();
88         ImageIO.write(image, "JPEG", response.getOutputStream());
89     %>

 

 

posted @ 2013-01-30 21:12  357  阅读(114)  评论(0)    收藏  举报