用java生成验证码

这是前些天闲着没事的时候做的一个验证码的程序,也参考了一些例子最近总结出来的.

java 代码
  1. public class Image extends HttpServlet {   
  2.   
  3.  /**  
  4.   *   
  5.   */  
  6.  private static final long serialVersionUID = 1L;   
  7.   
  8.  protected void processRequest(HttpServletRequest request,   
  9.    HttpServletResponse response) throws ServletException, IOException {   
  10.   
  11.   response.setContentType("image/jpeg");   
  12.   response.setHeader("Pragma""No-cache");   
  13.   response.setHeader("Cache-Control""no-cache");   
  14.   response.setDateHeader("Expires"0);   
  15.   HttpSession session = request.getSession();   
  16.   
  17.   int width = 60, height = 20;   
  18.   
  19.   BufferedImage image = new BufferedImage(width, height,   
  20.     BufferedImage.TYPE_INT_RGB);   
  21.   
  22.   // 获取图形上下文   
  23.   Graphics g = image.getGraphics();   
  24.   
  25.   // 生成随机类   
  26.   Random random = new Random();   
  27.   
  28.   // 设定背景色   
  29.   g.setColor(getRandColor(200250));   
  30.   g.fillRect(00, width, height);   
  31.   
  32.   // 设定字体   
  33.   g.setFont(new Font("Times New Roman", Font.PLAIN, 18));   
  34.   
  35.   // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到   
  36.   g.setColor(getRandColor(160200));   
  37.   for (int i = 0; i < 155; i++) {   
  38.    int x = random.nextInt(width);   
  39.    int y = random.nextInt(height);   
  40.    int xl = random.nextInt(12);   
  41.    int yl = random.nextInt(12);   
  42.    g.drawLine(x, y, x + xl, y + yl);   
  43.   }   
  44.   
  45.   // 取随机产生的认证码(4位数字)   
  46.   String sRand = "";   
  47.   for (int i = 0; i < 4; i++) {   
  48.    String rand = String.valueOf(random.nextInt(10));   
  49.    sRand += rand;   
  50.    // 将认证码显示到图象中   
  51.    g.setColor(new Color(20 + random.nextInt(110), 20 + random   
  52.      .nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成   
  53.    g.drawString(rand, 13 * i + 616);   
  54.   }   
  55.   
  56.   // 将认证码存入SESSION   
  57.   session.setAttribute("rand", sRand);   
  58.   // 图象生效   
  59.   g.dispose();   
  60.   ServletOutputStream responseOutputStream = response.getOutputStream();   
  61.   // 输出图象到页面   
  62.   ImageIO.write(image, "JPEG", responseOutputStream);   
  63.   
  64.   // 以下关闭输入流!   
  65.   responseOutputStream.flush();   
  66.   responseOutputStream.close();   
  67.   
  68.  }   
  69.   
  70.  Color getRandColor(int fc, int bc) {   
  71.   // 给定范围获得随机颜色   
  72.   Random random = new Random();   
  73.   if (fc > 255)   
  74.    fc = 255;   
  75.   if (bc > 255)   
  76.    bc = 255;   
  77.   int r = fc + random.nextInt(bc - fc);   
  78.   int g = fc + random.nextInt(bc - fc);   
  79.   int b = fc + random.nextInt(bc - fc);   
  80.   return new Color(r, g, b);   
  81.  }   
  82.   
  83.  /**  
  84.   * Handles the HTTP GET method.  
  85.   *   
  86.   * @param request  
  87.   *            servlet request  
  88.   * @param response  
  89.   *            servlet response  
  90.   */  
  91.  protected void doGet(HttpServletRequest request,   
  92.    HttpServletResponse response) throws ServletException, IOException {   
  93.   processRequest(request, response);   
  94.  }   
  95.   
  96.  /**  
  97.   * Handles the HTTP POST method.  
  98.   *   
  99.   * @param request  
  100.   *            servlet request  
  101.   * @param response  
  102.   *            servlet response  
  103.   */  
  104.  protected void doPost(HttpServletRequest request,   
  105.    HttpServletResponse response) throws ServletException, IOException {   
  106.   processRequest(request, response);   
  107.  }   
  108.   
  109.  /**  
  110.   * Returns a short description of the servlet.  
  111.   */  
  112.  public String getServletInfo() {   
  113.   return "Short description";   
  114.  }   
  115.   
  116. }   
  117.   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值