验证码的秘密

[size=medium][color=indigo]/**
* 随机生成四位验证码,可以给代码写在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();
}[/color][/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值