生成验证码工具类:ValidateCode.java
package com.singlee.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Random; import javax.imageio.ImageIO; /** * 验证码生成器 * */ public class ValidateCode { // 图片的宽度。 private int width = 80; // 图片的高度。 private int height = 35; // 验证码字符个数 private int codeCount = 4; // 验证码干扰线数 private int lineCount = 100; // 验证码 private String code = null; // 验证码图片Buffer private BufferedImage buffImg = null; private char[] codeSequence = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" .toCharArray(); public ValidateCode() { this.createCode(); } /** * * @param width * 图片宽 * @param height * 图片高 */ public ValidateCode(int width, int height) { this.width = width; this.height = height; this.createCode(); } /** * * @param width * 图片宽 * @param height * 图片高 * @param codeCount * 字符个数 * @param lineCount * 干扰线条数 */ public ValidateCode(int width, int height, int codeCount, int lineCount) { this.width = width; this.height = height; this.codeCount = codeCount; this.lineCount = lineCount; this.createCode(); } public void createCode() { int fontwidth = width / (codeCount+2);// 每个字符的宽度 int fontHeight = height - 4;// 字体的高度 int codeY = height - 8; // 图像buffer buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); // 将图像填充为白色 g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); // 创建字体 Font font = new Font("Arial", Font.PLAIN, fontHeight); g.setFont(font); // 生成随机数 Random random = new Random(); // 绘制干扰线 for (int i = 0; i < lineCount; i++) { int xs = random.nextInt(width); int ys = random.nextInt(height); int xe = xs + random.nextInt(width / 8); int ye = ys + random.nextInt(height / 8); // 随机颜色 g.setColor(getRandomColor()); g.drawLine(xs, ys, xe, ye); } // randomCode记录随机产生的验证码 StringBuffer randomCode = new StringBuffer(); // 随机产生codeCount个字符的验证码。 for (int i = 0; i < codeCount; i++) { String strRand = String.valueOf(codeSequence[random .nextInt(codeSequence.length)]); // 产生随机的颜色值,让输出的每个字符的颜色值都将不同。 g.setColor(getRandomColor()); g.drawString(strRand, (i + 1) * fontwidth, codeY); // 将产生的四个随机数组合在一起。 randomCode.append(strRand); } // 将四位数字的验证码保存到Session中。 code = randomCode.toString(); } /** * 获取一个随机的颜色 * * @return */ private Color getRandomColor() { Random random = new Random(); int r = random.nextInt(255); int g = random.nextInt(255); int b = random.nextInt(255); Color c = new Color(r, g, b); return c; } public void write(String path) throws IOException { OutputStream os = new FileOutputStream(path); this.write(os); } public void write(OutputStream os) throws IOException { ImageIO.write(buffImg, "png", os); os.close(); } /** * 获取图片BufferedImage * * @return */ public BufferedImage getBuffImg() { return buffImg; } /** * 获取验证码字符串 * * @return */ public String getCode() { return code; } }
处理验证码请求的控制层:ValidateCodeController.java
package com.singlee.controller; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.singlee.controller.common.BaseController; import com.singlee.model.common.ComException; import com.singlee.util.ValidateCode; /** * 验证码 * * @author Ming * */ @Controller @RequestMapping("/validatecode") public class ValidateCodeController extends BaseController { @RequestMapping("randomcode") public void randomCode(HttpServletRequest req, HttpServletResponse resp) throws ComException { // 设置响应的类型格式为图片格式 resp.setContentType("image/jpeg"); // 禁止图像缓存。 resp.setHeader("Pragma", "no-cache"); resp.setHeader("Cache-Control", "no-cache"); resp.setDateHeader("Expires", 0); HttpSession session = req.getSession(); ValidateCode vCode = new ValidateCode(80, 35, 4, 70); System.out.println("code:" + vCode.getCode()); //验证码保存到Session session.setAttribute("code", vCode.getCode()); try { vCode.write(resp.getOutputStream()); } catch (IOException e) { e.printStackTrace(); throw new ComException("输出验证码图片异常:"+e.getMessage()); } } }
页面显示:login.jsp
<div id="checkCodePanel" class="row"> <label id="f_checkCode" class="field" style="visibility: visible;color: #999"> 请输入右侧验证码 </label> <img class="checkImg" id='checkImg' src="<%=basePath%>validatecode/randomcode" /> <input name="checkCode" class="text_checkcode" type="text" id="checkCode" size="6" /> </div>
点击刷新验证码:login.js
// 单击验证码刷新 $('#checkImg').click(function() { $('#checkImg').attr('src', 'validatecode/randomcode?d=' + Math.random()); });
相关推荐
随机验证码。 - 随机生成十组六位字符组成的验证码。 - 验证码由大小写字母、数字字符组成。
用java编写的四位随机的验证码,包括数字跟字母 简单易懂
本文档讲述了如何使用java来随机的生成验证码
java识别验证码 仅限字母数字 附带bmp图片 验证码图片位置随机变动
android java 获取随机4位验证码(包含大小写字母,数字,不重复)
java随机产生验证码,可以随机生成数字、大写字母、小写字母。还可以随机生成文字字体、及大小。在图片上面可能字体都不不同、大小不等。
随机生成手机验证码的java源码文件,结果是在后台输出,可根据选择生成几位随机码或者随机码是否带有字母。
java生成四位随机数,包含数字和字母 区分大小写,特别适合做验证码,android开发
csdn上三种java验证码生成方法,以及谷歌开源验证码,总共四种验证码生成集合。将生成的四个验证图片放在一个页面上以供比对,全部可以运行。 四种方法分别为: 1. 数字、字母、汉字混合验证码,叠影(给力)、扭曲 ...
在我们项目中间经常会用到验证码...这是一个生成验证码的例子,直接可以用在项目中...生成带条纹的含有字母和数字的随机验证码!
使用SpringBoot搭建,jdk1.8,layui前端框架。验证码图片文字随机生成,可以选择中文、字母、数字、字母数字的组合。可以根据自己的需要进行修改。
java生成随机数字和字母验证码,
验证码通常是以一幅图片的形式显示的,用户按照图片中显示的数字或者字母依次输入,服务端将对用户输入和验证码进行比较,以判断用户是否经过检验。由于验证码都是随机生成的,自动发布信息的软件无法知道生成的...
这是一个支持数字,字母混合的随机验证码工具类,包含验证码校验等。
java 生成随机图片验证码 ,字母数组随机组合,加干扰线
java 产生随机验证码,servlet实现 点击图片可自动刷新验证码。 支持字母(可分大小写),数字
java随机产生验证码,可以随机生成数字、大写字母、小写字母。还可以随机生成文字字体、及大小。在图片上面可能字体都不不同、大小不等
验证码由大小写字母、数字字符组成。 * 48~57(0-9) * 65~90(A-Z) * 97~122(a-z)
在JSP页面中产生随机验证码的小例子 在JSP页面中产生随机验证码的小例子
本篇文章主要介绍了Java案例之随机验证码功能实现实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧