动态验证码生成

1:GIF 格式

/**

 * 获取验证码(Gif版本)

 * @param response

 */

@RequestMapping(value="getGifCode",method=RequestMethod.GET)

public void getGifCode(HttpServletResponse response,HttpServletRequest request){

 try {

 response.setHeader("Pragma", "No-cache"); 

 response.setHeader("Cache-Control", "no-cache"); 

 response.setDateHeader("Expires", 0); 

 response.setContentType("image/gif"); 

 /**

 * gif格式动画验证码

 * 宽,高,位数。

 */

 Captcha captcha = new GifCaptcha(146,33,4);

 //输出

 captcha.out(response.getOutputStream());

 HttpSession session = request.getSession(true); 

 //存入Session

 session.setAttribute("_code",captcha.text().toLowerCase()); 

 } catch (Exception e) {

 LoggerUtils.fmtError(getClass(),e, "获取验证码异常:%s",e.getMessage());

 }

}

2:jpg格式验证码

/**

 * 获取验证码(jpg版本)

 * @param response

 */

@RequestMapping(value="getJPGCode",method=RequestMethod.GET)

public void getJPGCode(HttpServletResponse response,HttpServletRequest request){

 try {

 response.setHeader("Pragma", "No-cache"); 

 response.setHeader("Cache-Control", "no-cache"); 

 response.setDateHeader("Expires", 0); 

 response.setContentType("image/jpg"); 

 /**

 * jgp格式验证码

 * 宽,高,位数。

 */

 Captcha captcha = new SpecCaptcha(146,33,4);

 //输出

 captcha.out(response.getOutputStream());

 HttpSession session = request.getSession(true); 

 //存入Session

 session.setAttribute("_code",captcha.text().toLowerCase()); 

 } catch (Exception e) {

 LoggerUtils.fmtError(getClass(),e, "获取验证码异常:%s",e.getMessage());

 }

}

附件工具类下载

你可能感兴趣的:(动态验证码生成)