public void verifyCode() throws IOException{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ValidatorCode codeUtil = ValidatorCodeUtil.getCode();
System.out.println("code="+codeUtil.getCode());
request.getSession().setAttribute("code", codeUtil.getCode());
// 禁止图像缓存。
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream sos = null;
try {
// 将图像输出到Servlet输出流中。
sos = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(codeUtil.getImage());
sos.flush();
sos.close();
} catch (Exception e) {
} finally {
if (null != sos) {
try {
sos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.w658.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
public class ValidatorCodeUtil
{
public static ValidatorCode getCode()
{
int width = 80;
int height = 30;
BufferedImage buffImg = new BufferedImage(width, height, 1);
Graphics2D g = buffImg.createGraphics();
// 创建一个随机数生成器类
Random random = new Random();
// 将图像填充为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// 创建字体,字体的大小
Font font = new Font("微软雅黑", 2, 28);
// 设置字体。
g.setFont(font);
// 画边框。
g.setColor(Color.LIGHT_GRAY);
g.drawRect(0, 0, width - 1, height - 1);
// 随机产生10条干扰线,使图象中的认证码不易被其它程序探测到。
//g.setColor(Color.lightGray);
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
for (int i = 0; i < 10; 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;
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int size = base.length();
// 随机产生codeCount数字的验证码。
for (int i = 0; i < length; i++)
{
int start = random.nextInt(size);
String strRand = base.substring(start, start + 1);
// 用随机产生的颜色将验证码绘制到图像中。
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;
}
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);
}
public static class ValidatorCode
{
private BufferedImage image;
private String code;
public BufferedImage getImage()
{
return this.image;
}
public String getCode()
{
return this.code;
}
}
}
package com.w658.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
public class ValidatorCodeUtil
{
public static ValidatorCode getCode()
{
int width = 80;
int height = 30;
BufferedImage buffImg = new BufferedImage(width, height, 1);
Graphics2D g = buffImg.createGraphics();
// 创建一个随机数生成器类
Random random = new Random();
// 将图像填充为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// 创建字体,字体的大小
Font font = new Font("微软雅黑", 2, 28);
// 设置字体。
g.setFont(font);
// 画边框。
g.setColor(Color.LIGHT_GRAY);
g.drawRect(0, 0, width - 1, height - 1);
// 随机产生10条干扰线,使图象中的认证码不易被其它程序探测到。
//g.setColor(Color.lightGray);
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
for (int i = 0; i < 10; 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;
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int size = base.length();
// 随机产生codeCount数字的验证码。
for (int i = 0; i < length; i++)
{
int start = random.nextInt(size);
String strRand = base.substring(start, start + 1);
// 用随机产生的颜色将验证码绘制到图像中。
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;
}
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);
}
public static class ValidatorCode
{
private BufferedImage image;
private String code;
public BufferedImage getImage()
{
return this.image;
}
public String getCode()
{
return this.code;
}
}
}
转载于:https://www.cnblogs.com/wx491592452/p/95494cde582487eef6cce21b817efdcc.html