JAVA验证码[含字母数字]
2011-06-29 14:47
344 查看
import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.*; import java.io.*; import javax.servlet.http.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RandomCode extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form. has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form. has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 验证码图片的宽度。 int width = 70; //验证码图片的高度。 int height = 36; BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); //创建一个随机数生成器类。 Random random = new Random(); // 设定图像背景色(因为是做背景,所以偏淡) g.setColor(getRandColor(180, 250)); g.fillRect(0, 0, width, height); //创建字体,字体的大小应该根据图片的高度来定。 Font font = new Font("Times New Roman", Font.PLAIN, 28); //设置字体。 g.setFont(font); //画边框。 g.setColor(Color.BLACK); g.drawRect(0, 0, width - 1, height - 1); // 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。 g.setColor(Color.GRAY); for (int i = 0; i < 50; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } //randomCode用于保存随机产生的验证码,以便用户登录后进行验证。 StringBuffer randomCode = new StringBuffer(); // 设置默认生成4个验证码 int length = 4; // 设置备选验证码:包括"a-z"和数字"0-9" String base = "abcdefghijklmnopqrstuvwxyz0123456789"; int size = base.length(); //随机产生4位数字的验证码。 for (int i = 0; i < length; i++) { //得到随机产生的验证码数字。 int start = random.nextInt(size); String strRand = base.substring(start, start + 1); //用随机产生的颜色将验证码绘制到图像中。 // g.setColor(new Color(red,green,blue)); // 生成随机颜色(因为是做前景,所以偏深) g.setColor(getRandColor(1, 100)); g.drawString(strRand, 13 * i + 6, 28); //将产生的四个随机数组合在一起。 randomCode.append(strRand); } //将四位数字的验证码保存到Session中。 HttpSession session = request.getSession(); session.setAttribute("rand", randomCode.toString()); //禁止图像缓存。 response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); //将图像输出到Servlet输出流中。 ServletOutputStream sos = response.getOutputStream(); ImageIO.write(buffImg, "jpeg", sos); sos.close(); } Color getRandColor(int fc, int bc) {//给定范围获得随机颜色 Random random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } }
相关文章推荐
- Java实现Web页面前数字字母验证码实现 推荐
- Java程序员从笨鸟到菜鸟(一)验证码实现,随机生成数字和字母、点击刷新、加入干扰线等样式
- Java web 开发: 随机生成验证码,支持大小写字母、数字;随机字体
- Java web 开发: 随机生成验证码,支持大小写字母、数字;随机字体
- java生成四位随机数,包含数字和字母 区分大小写,特别适合做验证码,android开发
- java验证码中的数字、字母、汉字如何随机生成
- 字母数字、字母、汉字验证码 (java)
- Java随机输出验证码包含数字、字母、汉字
- java随机生成数字字母验证码
- 字母数字、字母、汉字验证码 (java)
- Java随机生成字母数字组合的验证码
- 字母数字、字母、汉字验证码 (java)
- Java 随机生成六位数验证码过程(含大小写字母及数字)
- Java-验证码生成(数字+字母)
- Java web 开发: 随机生成验证码,支持大小写字母、数字;随机字体
- Java版纯字母、纯数字、字母数字组合验证码的生成
- java验证码(数字,数字和字母,汉字)源代码
- 字母数字、字母、汉字验证码 (java)
- [置顶] java生成四位随机数,包含数字和字母 区分大小写,特别适合做验证码,android开发
- Java 随机生成验证码,支持大小写字母、数字;随机字体