public class CheckCode extends HttpServlet {private static final long serialVersionUID = 1L;private int width = 80; // 验证码图片的宽private int height = 20; // 验证码图片的高private int codeCount = 4; // 验证码图片的字符数private int x = 16;private int fontHeight = 16;private int codeY = 18;private final char[] codeSequence = { ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘,‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘ };protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, java.io.IOException {// 构造一个类型为预定义图像类型之一的BufferedImage,设置图像的宽,高,和类型(TYPE_INT_RGB)BufferedImage Img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 返回Graphics2D,Graphics2D类扩展自Graphics 类,以提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制Graphics g = Img.getGraphics();Random random = new Random();// 将图像填充为白色g.setColor(Color.WHITE);// 填充指定的矩形。x,y坐标均为0,宽为width,高为heightg.fillRect(0, 0, width, height);// 创建字体,字体的大小应该根据图片的高度来定。Font font = new Font("Times new Roman", Font.PLAIN, fontHeight);g.setColor(Color.black);g.setFont(font);Color juneFont = new Color(153, 204, 102);// 随机产生130条干扰线,不易被其它程序探测g.setColor(juneFont);for (int i = 0; i < 130; i++) {// 返回伪随机数int x = random.nextInt(width);int y = random.nextInt(height);int xl = random.nextInt(16); // 80/5=16int yl = random.nextInt(16);// 在此图形上下文的坐标系中,使用当前颜色在点 (x1, y1) 和 (x2, y2) 之间画一条线g.drawLine(x, y, x + xl, y + yl);}// randomCode用于保存随机产生的验证码,以便用户登录后进行验证,线程安全的可变字符序列StringBuffer randomCode = new StringBuffer();// 随机产生codeCount数字的验证码for (int i = 0; i < codeCount; i++) {// 返回 char 参数的字符串表示形式String strRand = String.valueOf(codeSequence[random.nextInt(36)]);// 用随机产生的颜色将验证码绘制到图像中// 创建具有指定红色、绿色和蓝色值的不透明的 sRGB 颜色,这些值都在 (0 - 255) 的范围内g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));// 使用此图形上下文的当前字体和颜色绘制由指定 string 给定的文本。最左侧字符的基线位于此图形上下文坐标系的 (x, y)// 位置处。g.drawString(strRand, (i + 1) * x - 4, codeY);randomCode.append(strRand);}HttpSession session = request.getSession(); // 将四位数字的验证码保存到Session中session.setAttribute("realcode", randomCode.toString());// 禁止浏览器缓存response.setHeader("Pragma", "no-cache"); // HTTP 1.0response.setHeader("Cache-Control", "no-cache");// HTTP 1.1response.setDateHeader("Expires", 0); // 在代理服务器端防止缓冲response.setContentType("image/gif"); // 设置正被发往客户端的响应的内容类型// 将图像输出到Servlet输出流中,ServletOutputStream提供了向客户端发送二进制数据的输出流ServletOutputStream sos = response.getOutputStream();ImageIO.write(Img, "gif", sos); // 使用支持给定格式的任意 ImageWriter 将一个图像写入// OutputStreamsos.flush(); // 刷新此输出流并强制写出所有缓冲的输出字节sos.close();}
基于web的网上书城系统开发-----登录注册扩展-------验证码功能
原文地址:http://www.cnblogs.com/durui/p/7627226.html