生成base64格式图片验证码
/**
* 验证码的候选内容
*/
private char codeSequence[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
/**
* 生成验证码
* 改造生成验证码的方式,将图片base64形式传到前台,而不是直接传验证码到前台
* @return
* @throws IOException
*/
public void imageCode() throws IOException {
HttpServletResponse resp = CommandContext.getResponse();
HttpServletRequest req = CommandContext.getRequest();
String method= req.getMethod();
if("OPTIONS".equals(method)){
return ;
}
Map map=new HashMap(); // 在内存中创建图象
int width = 65, height = 38;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 生成随机类
Random random = new Random();
// 设定背景色
g.setColor(getRandColor(230, 255));
g.fillRect(0, 0, 100, 40);
// 设定字体
g.setFont(new Font("Arial", Font.CENTER_BASELINE | Font.ITALIC, 20));
// 产生0条干扰线,
g.drawLine(0, 0, 0, 0); //存放验证码
StringBuffer sRand = new StringBuffer();
for (int i = 0; i < charCount; i++) {
String singleCode = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
sRand.append(singleCode);
// 将认证码显示到图象中
g.setColor(getRandColor(100, 150));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(singleCode, 14 * i + 5, 25);
}
for(int i=0;i<(random.nextInt(5)+5);i++){
g.setColor(new Color(random.nextInt(255)+1,random.nextInt(255)+1,random.nextInt(255)+1));
g.drawLine(random.nextInt(70),random.nextInt(40),random.nextInt(70),random.nextInt(40));
g.drawLine(random.nextInt(70),random.nextInt(40),random.nextInt(70),random.nextInt(40));
} HttpSession session = req.getSession();
//获取clientid
String clientId= SystemUtil.getClientId(req);
if(StringUtils.isEmpty(clientId)){
//生成clientid
String userAgent=req.getHeader("User-Agent");
String sessionId=session.getId();
String cip= IpPolicy.getClientIP(req);
clientId=CodeUtil.genClientId(sessionId,cip,userAgent);
}
map.put("clientId", clientId);
if (isValidateCodeCaseSensitive) {
session.setAttribute("randomCode", sRand.toString());
SystemUtil.push2Cache(clientId, sRand.toString());
} else {
session.setAttribute("randomCode", sRand.toString().toLowerCase());
SystemUtil.push2Cache(clientId, sRand.toString().toLowerCase());
}
// 图象生效
g.dispose();
try{ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", outputStream);
BASE64Encoder encoder = new BASE64Encoder();
String base64Img = encoder.encode(outputStream.toByteArray());
base64Img="data:image/jpg;base64, "+base64Img.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n;
map.put("verCode", base64Img);
Object jsonObj = JSONSerializer.toJSON(map);
byte[] json = jsonObj.toString().getBytes("UTF-8");
resp.setContentType("text/plain;chartset=utf-8");
resp.setHeader("Cache-Control", "no-cache");
resp.setHeader("Expires", "0");
resp.setIntHeader("Content-Length", json.length);
ServletOutputStream responseOutputStream = resp.getOutputStream();
responseOutputStream.write(json);
// 以下关闭输入流!
responseOutputStream.flush();
responseOutputStream.close();
// 获得页面key值
return ;
} catch (IOException e) {
logger.error("生产验证码出错",e);
throw new SystemException("生产验证码出错",e);
}
} /**
* 给定范围获得随机颜色
*
* @param fc
* @param bc
* @return
*/
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);
}