java 实现验证码

  1 package edu.zzuli.common;
  2 
  3 import java.awt.Color;
  4 import java.awt.Font;
  5 import java.awt.Graphics2D;
  6 import java.awt.image.BufferedImage;
  7 import java.io.IOException;
  8 import java.util.Random;
  9 
 10 import javax.imageio.ImageIO;
 11 import javax.servlet.ServletException;
 12 import javax.servlet.http.HttpServlet;
 13 import javax.servlet.http.HttpServletRequest;
 14 import javax.servlet.http.HttpServletResponse;
 15 
 16 
 17 /**
 18  * @author tianshaojie
 19  * @date 2011-1-7
 20  * @discription :
 21  */
 22 public class VerifyCodeServlet extends HttpServlet {
 23         
 24         private static final long serialVersionUID = -7713673953332628094L;
 25 
 26         private Font mFont = new Font("Arial Black", Font.BOLD, 15); // 设置字体new  Font("Arial Black",  Font.PLAIN, 15);
 27 
 28         private int lineWidth = 5; // 2// 干扰线的长度=1.414*lineWidth
 29 
 30         private int width = 60; // 定义图形大小
 31 
 32         private int height = 20; // 定义图形大小
 33 
 34         private int count = 300;// 200
 35 
 36         private Color getRandColor(int fc, int bc) { // 取得给定范围随机颜色
 37                 Random random = new Random();
 38                 if (fc > 255) {
 39                         fc = 255;
 40                 }
 41                 if (bc > 255) {
 42                         bc = 255;
 43                 }
 44                 int r = fc + random.nextInt(bc - fc);
 45 
 46                 int g = fc + random.nextInt(bc - fc);
 47                 int b = fc + random.nextInt(bc - fc);
 48                 return new Color(r, g, b);
 49         }
 50 
 51         @SuppressWarnings("unused")
 52         private char random2Char(int random) {
 53                 char c = '0';
 54                 if (random >= 0 && random <= 9) {
 55                         c = (char) (random + 48);
 56                 } else if (random >= 10 && random <= 35) {
 57                         c = (char) (random + 55);
 58                 } else if (random >= 36 && random <= 61) {
 59                         c = (char) (random + 61);
 60                 }
 61                 return c;
 62         }
 63 
 64         public void doPost(HttpServletRequest request, HttpServletResponse response)
 65                         throws ServletException, IOException {
 66                 doGet(request, response);
 67         }
 68 
 69         public void doGet(HttpServletRequest request, HttpServletResponse response)
 70                         throws ServletException, IOException {
 71                 // response.reset();
 72                 // 设置页面不缓存
 73                 response.setHeader("Pragma", "No-cache");
 74                 response.setHeader("Cache-Control", "no-cache");
 75                 response.setDateHeader("Expires", 0);
 76                 response.setContentType("image/gif");
 77                 // 在内存中创建图象
 78                 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
 79 
 80                 // 获取图形上下文
 81                 Graphics2D g = (Graphics2D) image.getGraphics();
 82                 // 生成随机类
 83                 Random random = new Random();
 84                 // 设定背景色
 85                 g.setColor(getRandColor(200, 250)); // ---1
 86                 g.fillRect(0, 0, width, height);
 87 
 88                 // 设定字体
 89                 g.setFont(mFont);
 90 
 91                 // 画边框
 92                 g.setColor(getRandColor(0, 20)); // ---2
 93                 g.drawRect(0, 0, width - 1, height - 1);
 94 
 95                 // 随机产生干扰线,使图象中的认证码不易被其它程序探测到
 96                 for (int i = 0; i < count; i++) {
 97                         g.setColor(getRandColor(150, 200)); // ---3
 98                         int x = random.nextInt(width - lineWidth - 1) + 1; // 保证画在边框之内
 99                         int y = random.nextInt(height - lineWidth - 1) + 1;
100                         int xl = random.nextInt(lineWidth);
101                         int yl = random.nextInt(lineWidth);
102                         g.drawLine(x, y, x + xl, y + yl);
103                 }
104 
105                 // 取随机产生的认证码(4位数字)
106                 String sRand = "";
107 
108                 for (int i = 0; i < 4; i++) {
109                         // String rand = String.valueOf(random2Char(random.nextInt(62)));
110                         String rand = String.valueOf(random.nextInt(10));
111                         sRand += rand;
112                         // 将认证码显示到图象中,调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
113                         g.setColor(new Color(20 + random.nextInt(130), 20 + random.nextInt(130), 20 + random.nextInt(130))); // --4--50-100
114                         g.drawString(rand, (13 * i) + 6, 16);
115                 }
116                 // 将认证码存入SESSION
117                 request.getSession().setAttribute(Constants_core.RANDOM_CHECKCODE, sRand);
118                 // System.out.println("checkcode:"+sRand);
119                 // 图象生效
120                 g.dispose();
121                 // 输出图象到页面
122                 ImageIO.write(image, "PNG", response.getOutputStream());
123         }
124 
125         public static String randomCheckCode(HttpServletRequest request) {
126                 Random random = new Random();
127                 String sRand = "";
128                 for (int i = 0; i < 4; i++) {
129                         String rand = String.valueOf(random.nextInt(10));
130                         sRand += rand;
131                 }
132                 request.getSession().setAttribute(Constants_core.RANDOM_CHECKCODE, sRand);
133                 return sRand;
134         }
135 
136 }

 

posted @ 2014-03-17 18:16  少爵  阅读(553)  评论(0编辑  收藏  举报