验证码的秘密

/**
* 随机生成四位验证码,可以给代码写在servlet 里,将验证码保存在servlet类中 
     *   request.getSession().setAttribute("piccode", sb.toString());
     *   request.getSession().removeAttribute(sb.toString());
     *   ImageIO.write(img, "JPG", response.getOutputStream());
     *    用户输入的验证码 可以和session的属性做判断。
*/
public String GetImage()
{
BufferedImage img = new BufferedImage(63, 21,BufferedImage.TYPE_INT_BGR);
         Graphics g = img.getGraphics();
         Random r = new Random();
         Color c = new Color(200, 150, 255);
         g.setColor(c);
         g.fillRect(0, 0,63, 21);
         StringBuffer sb = new StringBuffer();
         char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZqwertyuioplkjhgfdsazxcvbnm0123456789".toCharArray();
         int index=0, len = ch.length;
         String verifyCode="";
         for (int i = 0; i < 4; i ++) {
          index = r.nextInt(len);
          g.setColor(new Color(r.nextInt(88), r.nextInt(188), r.nextInt(255)));
          g.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 18));
          //drawString(String str, int x, int y)  的后两个参数:最左侧字符的基线位于此图形上下文坐标系统的 (x, y) 位置处。
          g.drawString(""+ch[index] , (i * 15) + 3, 18);
          verifyCode+=ch[index];
          sb.append(ch[index]);
         
     }
        
   return sb.toString();
}

你可能感兴趣的:(C++,c,servlet,C#)