1.jsp页面显示验证码,src跳转到Controller里的:getImg_validate,reloadImg()作用是点击验证码就会换一张(重新请求getImg_validate)
<ul> <li >验证码: </li> <li > <img id="img_validate" src="<%=basePath%>image/getImg_validate" onclick="reloadImg()"> <input type="text" name="yanzhengma" id="yanzhengma" placeholder="请输入验证码"/> </li> </ul> <script type="text/javascript"> function reloadImg() { document.getElementById("img_validate").src="<%=basePath%>image/getImg_validate"; } </script>
2.写一个ImageUtil工具类,在里面可以自定义图片生成的大小,字体大小,背景色,验证码个数,干扰线条条数等等
package com.svse.house.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.Random; public class ImageUtil { //1.定义变量保存生成后的验证码字符串 static String code = ""; //2.生成验证码 public static String createcode() { code = ""; String a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";//随机生成的字符范围(0-9,a-z,A-Z) //生成验证码的位数(这里是4位) for (int i = 0; i < 4; i++) { int index = (int) (Math.random() * 62);//会产生一个[0,62)的数,不包含小数 char b = a.charAt(index); code = code + b; } return code; } //查看已经生成的验证码 public static String getCode() { return code; } //3.生成图片 public static BufferedImage createimage() { // 前2个参数为:width, height.后面是图像类型 //创建一个不带透明色的BufferedImage对象,TYPE_INT_ARGB为带透明色 BufferedImage bi = new BufferedImage(130,50, BufferedImage.TYPE_INT_RGB); //1.得到一个画布 Graphics g = bi.getGraphics(); //2.添加背景颜色 g.setColor(Color.WHITE); g.fillRect(0, 0, 130, 50); //3.添加干扰线 for (int i = 0; i < 10; i++) { Random r = new Random(); int red = r.nextInt(256); int green = r.nextInt(256); int blue = r.nextInt(256); Color c = new Color(red, green, blue); g.setColor(c); int x1 = r.nextInt(131); int y1 = r.nextInt(51); int x2 = r.nextInt(131); int y2 = r.nextInt(51); g.drawLine(x1,y1,x2,y2);//画线 //g.drawOval(x1, y1, x2, y2);//画曲线 } //3.添加文字 g.setColor(Color.BLACK); g.setFont(new Font("宋体", Font.BOLD, 50)); String str = getCode(); //4.将文字填充到画板中 g.drawString(str, 15, 40); //5.关闭画布 g.dispose(); return bi; } }
3.在controller层调用getImg_validate,试用过jpeg和bmp格式的setContentType,都可以
package com.svse.house.controller; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.svse.house.util.ImageUtil; @Controller @RequestMapping("/image") public class ImageController { @RequestMapping("/getImg_validate") public void yanzheng(HttpServletResponse response){ try { ImageUtil.createcode(); BufferedImage image = ImageUtil.createimage(); //以流的方式返回给客户端 response.setContentType("image/jpeg"); //response.setContentType("image/bmp"); ByteArrayOutputStream bt = new ByteArrayOutputStream(); //将图片转换成字节流 ImageIO.write(image,"jpeg",bt); //ImageIO.write(image,"bmp",bt); //得到输出流,返回客户端 ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(bt.toByteArray()); } catch (IOException e) { e.printStackTrace(); } } }
4.最后的验证
当时一直想在前台获取随机生成的四位数验证码来和自己输入的进行比较,可以通过session来获取但如果刷新验证码session里的值不会改变除非刷新整个页面。
后来想想真傻,直接在后台比较不就好了吗。