java servlet 实现字母加(+)数字的验证码



java servlet 实现字母加(+)数字的验证码

Java代码
  1. import java.awt.Color;  
  2. import java.awt.Font;  
  3. import java.awt.Graphics;  
  4. import java.awt.image.BufferedImage;  
  5. import java.io.IOException;  
  6. import java.util.Random;  
  7. import javax.imageio.ImageIO;  
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.http.HttpServlet;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12. import javax.servlet.http.HttpSession;  
  13. publicclass AuthImg extends HttpServlet {  
  14. // 设置图形验证码中字符串的字体和大小
  15. private Font font = new Font(“Arial Black”, Font.PLAIN, 16);  
  16. publicvoid init() throws ServletException {  
  17. super.init();  
  18. }  
  19. // 生成随机颜色
  20. Color getRandColor(int fc, int bc) {  
  21.    Random random = new Random();  
  22. if (fc < 255) {  
  23.     fc = 255;  
  24.    }  
  25. if (bc < 255) {  
  26.     bc = 255;  
  27.    }  
  28. int r = fc + random.nextInt(bc - fc);  
  29. int g = fc + random.nextInt(bc - fc);  
  30. int b = fc + random.nextInt(bc - fc);  
  31. returnnew Color(r, g, b);  
  32. }  
  33. @Override
  34. protectedvoid service(HttpServletRequest request,  
  35.     HttpServletResponse response) throws ServletException, IOException {  
  36. //
  37.    response.setHeader(“Pragma”“No-cache”);  
  38.    response.setHeader(“Cache-Control”“No-cache”);  
  39.    response.setDateHeader(“Expires”0);  
  40.    response.setContentType(“image/jpeg”);  
  41. // 指定图形验证码图片的大小
  42. int width = 100, height = 18;  
  43. // 生成一张新图片
  44.    BufferedImage image = new BufferedImage(width, height,  
  45.      BufferedImage.TYPE_INT_RGB);  
  46. // 在图片中绘制内容
  47.    Graphics g = image.getGraphics();  
  48.    Random random = new Random();  
  49.    g.setColor(getRandColor(200250));  
  50.    g.fillRect(11, width - 1, height - 1);  
  51.    g.setColor(new Color(102102102));  
  52.    g.drawRect(00, width - 1, height - 1);  
  53.    g.setFont(font);  
  54. // 随机生成线条
  55.    g.setColor(getRandColor(160200));  
  56. for (int i = 0; i < span>155; i++) {  
  57. int x1 = random.nextInt(6) + 1;  
  58. int y1 = random.nextInt(12) + 1;  
  59. int x2 = random.nextInt(width - 1);  
  60. int y2 = random.nextInt(width - 1);  
  61.     g.drawLine(x1, y1, x1 + x2, y1 + y2);  
  62.    }  
  63. // 随机生成线条
  64. for (int i = 0; i < span>70; i++) {  
  65. int x1 = random.nextInt(6) + 1;  
  66. int y1 = random.nextInt(12) + 1;  
  67. int x2 = random.nextInt(width - 1);  
  68. int y2 = random.nextInt(width - 1);  
  69.     g.drawLine(x1, y1, x1 - x2, y1 - y2);  
  70.    }  
  71. // 该变量用于保存系统生成的随机字符串
  72.    String randStr = “”;  
  73. for (int i = 0; i < span>6; i++) {  
  74. // 获得一个随机字符
  75.     String tmp = getRandChar();  
  76.     randStr += tmp;  
  77. // 将系统生成的随机字符添加到图形验证码上
  78.     g.setColor(new Color(20 + random.nextInt(110), 20 + random  
  79.       .nextInt(110), 20 + random.nextInt(110)));  
  80.     g.drawString(tmp, 15 * i + 1015);  
  81.    }  
  82.    HttpSession session = request.getSession(true);  
  83. // 将系统生成的图形验证码放到Session中
  84.    session.setAttribute(“rand”, randStr);  
  85.    g.dispose();  
  86. // 输出图形验证码
  87.    ImageIO.write(image, “JPEG”, response.getOutputStream());  
  88. }  
  89. // 生成随机字符
  90. private String getRandChar() {  
  91. int rand = (int) Math.round(Math.random() * 2);  
  92. long itmp = 0;  
  93. char ctmp = ‘\u0000′;  
  94. // 根据rand的值来决定来生成一个大写字母、小写字母和数字
  95. switch (rand) {  
  96. // 生成大写字母
  97. case1:  
  98.     itmp = Math.round(Math.random() * 25 + 65);  
  99.     ctmp = (char) itmp;  
  100. return String.valueOf(ctmp);  
  101. // 生成小写字母
  102. case2:  
  103.     itmp = Math.round(Math.random() * 25 + 90);  
  104.     ctmp = (char) itmp;  
  105. return String.valueOf(ctmp);  
  106. // 生成数字
  107. default:  
  108.     itmp = Math.round(Math.random() * 9);  
  109. return String.valueOf(itmp);  
  110.    }  
  111. }  
  112. }  
  113. 页面上调用:  
  114. 请输入验证码:  
  115.    ”text”<  
  116.    ”AuthImg” id=“authImg” onclick=“this.src=’AuthImg?now=’+new Date()” alt=“点击刷新” >

 

 

  1. http://blog.163.com/gis_warrior/blog/#m=0&t=1&c=fks_084071092081081067084087083095085095082069085081087068087