图片验证码

来源:互联网 发布:美拍怎么加淘宝链接 编辑:程序博客网 时间:2024/04/28 00:51


前台页面:

<li><span><em>*</em>验证码:</span><input type="text" class="input-txt yzm-w" name="imgCode" placeholder="请输入图片验证码"/><img src="/user/getVerifyCode" class="yzm-w-pic" height="30" width="108" onclick="this.src='/user/getVerifyCode?t='+new Date().getTime()"/><a class="yzm-a" onclick="$(this).prev().attr('src', '/user/getVerifyCode?t='+new Date().getTime())">换一换</a></li>



后台请求(本文中用的springMVC):

/** * 获取图片验证码 * @return */@RequestMapping(value="/getVerifyCode")public ModelAndView getVerifyCode(HttpServletRequest request, HttpServletResponse response) {response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0);response.setContentType("image/jpeg");try {//生成随机字串String verifyCode = VerifyCodeUtils.generateVerifyCode(4);//生成验证码放入缓存redisString objType = "唯一的业务key";redis.set(objType,verifyCode,"超时时间");VerifyCodeUtils.outputImage(VerifyCodeUtils.VERIFY_WIDTH, VerifyCodeUtils.VERIFY_HEIGHT, response.getOutputStream(), verifyCode);//操作日志} catch (IOException e) {//此处可以抛出一个自定义验证码异常拦截e.printStackTrace();try {//生成随机字串String verifyCode = VerifyCodeUtils.generateVerifyCode(4);//生成验证码放入缓存redisString objType = "唯一的业务key";redis.set(objType,verifyCode,"超时时间");VerifyCodeUtils.outputImage(VerifyCodeUtils.VERIFY_WIDTH, VerifyCodeUtils.VERIFY_HEIGHT, response.getOutputStream(), verifyCode);//操作日志} catch (IOException e1) {e1.printStackTrace();}}return null;}


上面用到的工具类,点击打开链接下载!





0 0
原创粉丝点击