[jsp]代码库
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");//生成时的图像格式
String randomString=getRandomString();//获取随机数
request.getSession(true).setAttribute("randomString", randomString);
//生成的验证码图像的大小
int width=100;
int height=30;
Color color=getRandomColor();
Color reverse=getgetReverseColor(color);
BufferedImage bi=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g=bi.createGraphics();
g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16));
g.setColor(color);
g.fillRect(0, 0, width, height);
g.setColor(reverse);
g.drawString(randomString, 18, 20);
//用来产生污点来防止被破解
for(int i=0,n=random.nextInt(100);i<n;i++){
g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
}
ServletOutputStream out=response.getOutputStream();
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
out.flush();
}
高级设计师
by: 小蜜锋 发表于:2012-12-12 10:33:59 顶(1) | 踩(0) 回复
调用方法:
回复评论