含字母、数字的Servlet图形验证码

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