一、验证码工具类方法
/**
* 算术表达式验证码
*
* 1:干扰线的产生
* 2: 范围随机颜色,随机数
*
* @param response
* @return
*/
public static String drawImageVerificate(HttpServletResponse response){
//定义验证码的宽度和高度
int width = 100,height = 30;
//在内存中创建图片
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//创建图片的上下文
Graphics2D g = image.createGraphics();
//产生随机对象,此随机对象主要用于算术表达式的数字
Random random = new Random();
//设置背景
g.setColor(getRandomColor(240,250));
//设置字体
g.setFont(new Font("微软雅黑", Font.PLAIN,22));
//开始绘制
g.fillRect(0,0,width,height);
//干扰线的绘制,绘制线条到图片中
g.setColor(getRandomColor(180,230));
for(int i=0;i<10;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(60);
int y1 = random.nextInt(60);
g.drawLine(x,y,x1,y1);
}
//开始进行对算术验证码表达式的拼接
int num1 = (int)(Math.random()*10 + 1);
int num2 = (int)(Math.random()*10 + 1);
int fuhao = random.nextInt(3);//产生一个[0,2]之间的随机整数
//记录符号
String fuhaostr = null;
int result = 0;
switch (fuhao){
case 0 : fuhaostr = "+";result = num1 + num2;break;
case 1: fuhaostr = "-";result = num1 - num2;break;
case 2 : fuhaostr = "*";result = num1 * num2;break;
//case 3 : fuhaostr = "/";result = num1 / num2;break;
}
//拼接算术表达式,用户图片显示。
String calc = num1 + " " + fuhaostr +" "+ num2 +" = ?";
//设置随机颜色
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
//绘制表达式
g.drawString(calc,5,25);
//结束绘制
g.dispose();
try {
//输出图片到页面
ImageIO.write(image,"JPEG",response.getOutputStream());
return String.valueOf(result);
}catch (Exception ex){
ex.printStackTrace();
return null;
}
}
/**
* 范围随机颜色
* @param fc
* @param bc
* @return
*/
public static Color getRandomColor(int fc,int bc){
//利用随机数
Random random = new Random();
//随机颜色,了解颜色-Color(red,green,blue).rgb三元色 0-255
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);
}
二、jsp页面实现
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>java验证码</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<img src="code.jsp" alt="" id="code">
<a href="javascript:void(0);" onclick="changeCode()">看不清?点我</a>
<script>
function changeCode() {
document.getElementById("code").src = "code.jsp?d="+new Date().getTime();
}
</script>
</body>
</html>
code.jsp
<%@ page import="com.imooc.code.CaptcahCode" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
//1:清空浏览器缓存,目的是为了清空浏览器的缓存,因为浏览器
//会对网站的资源文件和图像进行记忆存储,如果被浏览器加载过的图片就记忆起来,记忆以后
//文件就不会和服务器在交互,如果我们验证不清空的话可能会造成一个问题就是:验证刷新以后没有效果。
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
response.setHeader("expires","0");
//2:调用编写的生成验证码的工具
String code = CaptcahCode.drawImageVerificate(response);
session.setAttribute("code",code);
//3:如何解决getOutputStream异常问题
out.clear();
out = pageContext.pushBody();
%>
实现效果: