前提
在这个互联网的时代,验证码的身影已经出现在各个角落里,验证码在我们使用各类网站的时候也发挥这不可替代的作用,时时刻刻保证用户的权益。可能关于验证码设置最厉害的也就是12306吧。用户没有被堵塞到购票的大军中,而是阻拦到登录的验证图上,关于验证码陪我们走过的那些段子,我就是不说了,直接奔向主题,在Java类中实现验证码的生成。
代码篇
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
//验证码的Action
public class CheckImgAction extends ActionSupport{
@Override
public String execute() throws Exception{
int width = 120;
int height = 30;
// 步骤一 绘制一张内存中图片
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 步骤二 图片绘制背景颜色 ---通过绘图对象
Graphics graphics = bufferedImage.getGraphics();// 得到画图对象 --- 画笔
// 绘制任何图形之前 都必须指定一个颜色
graphics.setColor(getRandColor(200, 250));
graphics.fillRect(0, 0, width, height);
// 步骤三 绘制边框
graphics.setColor(Color.WHITE);
graphics.drawRect(0, 0, width - 1, height - 1);
// 步骤四 四个随机数字
Graphics2D graphics2d = (Graphics2D) graphics;
// 设置输出字体
graphics2d.setFont(new Font("宋体", Font.BOLD, 18));
//ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890
//验证码中产生随机数内容的来源
String words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
Random random = new Random();// 生成随机数
// 定义StringBuffer
StringBuffer sb = new StringBuffer();
// 定义x坐标
int x = 10;
//产生验证码的位数
for (int i = 0; i < 4; i++) {
// 随机颜色
graphics2d.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
// 旋转 -30 --- 30度
int jiaodu = random.nextInt(60) - 30;
// 换算弧度
double theta = jiaodu * Math.PI / 180;
// 生成一个随机数字
int index = random.nextInt(words.length()); // 生成随机数 0 到 length - 1
// 获得字母数字
char c = words.charAt(index);
sb.append(c);
// 将c 输出到图片
graphics2d.rotate(theta, x, 20);
graphics2d.drawString(String.valueOf(c), x, 20);
graphics2d.rotate(-theta, x, 20);
x += 30;
}
// 将生成的字母存入到session中
ServletActionContext.getRequest().getSession()
.setAttribute("checkcode", sb.toString());
// 步骤五 绘制干扰线
graphics.setColor(getRandColor(160, 200));
int x1;
int x2;
int y1;
int y2;
for (int i = 0; i < 30; i++) {
x1 = random.nextInt(width);
x2 = random.nextInt(12);
y1 = random.nextInt(height);
y2 = random.nextInt(12);
graphics.drawLine(x1, y1, x1 + x2, x2 + y2);
}
// 将上面图片输出到浏览器 ImageIO
graphics.dispose();// 释放资源
ImageIO.write(bufferedImage, "jpg", ServletActionContext.getResponse()
.getOutputStream());
return NONE;
}
/**
* 取其某一范围的color
*
* @param fc
* int 范围参数1
* @param bc
* int 范围参数2
* @return Color
*/
private 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);
}
}
提示
其实当大家遇到验证码类似的功能直接用上面的内容就行了。但是有两个地方提示一下。1、验证码显示内容
2、验证码的字符长度。
可以根据自己的需要修改,让验证码的显示符合大家的需求,当然也可以根据需要修改字符显示的角度之类的内容。
问题篇
小编在做验证码点击更换的时候产生一个问题,OgnlValueStack:64
OgnlValueStack:64 - Error setting value
java.lang.NumberFormatException: For input string: "1487296589870"
其实这个问题一看就能明白,字符长度超出范围。但是何处来此错误?原因:在产生验证码的时候加了一个时间戳,但是没有地方给时间戳赋值,所以就溢出了。
产生错误的代码
function change(){
var img1 = document.getElementById("checkImg");
img1.src="${pageContext.request.contextPath}/checkImg.action?"+new Date().getTime();
}
修正后的代码
function change(){
var img1 = document.getElementById("checkImg");
img1.src="${pageContext.request.contextPath}/checkImg.action?time="+new Date().getTime();
}
总结