在发送短信的时候,一般需要增加图片验证码功能,用以防刷。这里介绍下验证码生成:
请求方法:
/**
* 获取生成图片验证码
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping("/getImageCode")
public void getImageCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 获取生成验证码图片
ValidatorCode imageCode = ValidatorCodeUtil.getCode();
logger.info("------ 生成的图片验证码为: " + imageCode.getCode());
// 放入session中
request.getSession().setAttribute("imageCode", imageCode.getCode());
// 禁止图像缓存
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
// 输出流
ServletOutputStream out = null;
try {
// 将图片输出到Servlet输出流
out = response.getOutputStream();
ImageIO.write(imageCode.getImage(), "jpg", out);
out.flush();
out.close();
} catch (Exception e) {
// 异常信息
String errMsg = printException(e);
logger.error("------ 生成图片验证码异常: " + errMsg);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
工具类:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
/**
* 图片验证码生成工具类
*
*/
public class ValidatorCodeUtil {
/**
* 生成验证码图片
* @return
*/
public static ValidatorCode getCode() {
// 图片宽度
int width = 80;
// 图片高度
int height = 30;
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
// 随机数生成器
Random random = new Random();
// 图像背景色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// 字体大小和字体
Font font = new Font("微软雅黑", Font.HANGING_BASELINE, 28);
g.setFont(font);
// 画边框
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到。
g.setColor(Color.GRAY);
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; 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);
}
// 用于保存随机产生的验证码
StringBuffer randomCode = new StringBuffer();
// 验证码位数
int length = 4;
// 设置备选验证码:包括"a-z"和数字"0-9"
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
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(getRandColor(1, 100));
// 调用函数出来的颜色相同
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
g.drawString(strRand, 15 * i + 6, 24);
// 将产生的四个随机数组合在一起
randomCode.append(strRand);
}
// 图象生效
g.dispose();
ValidatorCode code = new ValidatorCode();
code.image = buffImg;
code.code = randomCode.toString();
return code;
}
/**
* 指定范围获得随机颜色
* @param fc
* @param bc
* @return
*/
private static 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);
}
/**
* 验证码图片封装
* @author zhangdonglei
*
*/
public static class ValidatorCode {
private BufferedImage image;
private String code;
public BufferedImage getImage() {
return image;
}
public String getCode() {
return code;
}
}
}