首页 文章 精选 留言 我的

精选列表

搜索[数字主权],共10000篇文章
优秀的个人博客,低调大师

Java实现Web页面前数字字母验证码实现

最近公司做项目开发中用到了验证码实现功能,将实现代码分享出来, 前段页面实现代码: <ul> <li><label>验证码:</label></li> <li> <div> <inputtype="text"name="validateCode"class="login_input"value=""/> <imgid="img"src="${baseURL}/validateCode"onclick="onclickValidateCode(this);"/> </div> </li> </ul> 为了表达清晰,样式部分代码去掉了,大家根据自己的需求,自己添加样式。 页面JS代码:触发变动验证码改变的JS <scripttype="text/javascript"language="javascript"> //请求获取验证码 functiononclickValidateCode(obj){ $(obj).attr("src","${baseURL}/validateCode?"+newDate().getTime()); } </script> 后台 Controller处理: packagecom.njcc.pay.controller.login; importjava.awt.Color; importjava.awt.Font; importjava.awt.Graphics; importjava.awt.p_w_picpath.BufferedImage; importjava.io.IOException; importjava.io.OutputStream; importjava.util.Random; importjavax.p_w_picpathio.ImageIO; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importorg.apache.commons.lang3.math.NumberUtils; importorg.apache.commons.logging.Log; importorg.apache.commons.logging.LogFactory; importorg.springframework.stereotype.Controller; importorg.springframework.web.bind.annotation.RequestMapping; importcom.alibaba.dubbo.common.utils.StringUtils; /** *验证马Controller * *@authorAdministrator * */ @Controller publicclassValidateCodeController{ @SuppressWarnings("unused") privatestaticfinalLogLOG=LogFactory.getLog(ValidateCodeController.class); publicstaticfinalStringVALIDATE_CODE="validateCode"; privateintw=70; privateinth=23; /** *@throwsException *函数功能说明:进入后台登陆页面. * *@参数:@return *@returnString *@throws */ @RequestMapping(value="/validateCode") publicvoidvalidateCode(HttpServletRequestrequest, HttpServletResponseresponse)throwsException{ createImage(request,response); } privatevoidcreateImage(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException{ response.setHeader("Pragma","no-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires",0); response.setContentType("p_w_picpath/jpeg"); Stringwidth=request.getParameter("width"); Stringheight=request.getParameter("height"); if(StringUtils.isNumeric(width)&&StringUtils.isNumeric(height)){ w=NumberUtils.toInt(width); h=NumberUtils.toInt(height); } BufferedImagep_w_picpath=newBufferedImage(w,h,BufferedImage.TYPE_INT_RGB); Graphicsg=p_w_picpath.getGraphics(); /* *生成背景 */ createBackground(g); /* *生成字符 */ Strings=createCharacter(g); request.getSession().setAttribute(VALIDATE_CODE,s); g.dispose(); OutputStreamout=response.getOutputStream(); ImageIO.write(p_w_picpath,"JPEG",out); out.close(); } /** *生成颜色 *@paramfc *@parambc *@return */ privateColorgetRandColor(intfc,intbc){ intf=fc; intb=bc; Randomrandom=newRandom(); if(f>255){ f=255; } if(b>255){ b=255; } returnnewColor(f+random.nextInt(b-f),f+random.nextInt(b-f),f+random.nextInt(b-f)); } /** *生成背景 *@paramg */ privatevoidcreateBackground(Graphicsg){ //填充背景 g.setColor(getRandColor(220,250)); g.fillRect(0,0,w,h); //加入干扰线条 for(inti=0;i<8;i++){ g.setColor(getRandColor(40,150)); Randomrandom=newRandom(); intx=random.nextInt(w); inty=random.nextInt(h); intx1=random.nextInt(w); inty1=random.nextInt(h); g.drawLine(x,y,x1,y1); } } /** *生成字符 *@paramg *@return */ privateStringcreateCharacter(Graphicsg){ char[]codeSeq={'A','B','C','D','E','F','G','H','J', 'K','M','N','P','Q','R','S','T','U','V','W', 'X','Y','Z','2','3','4','5','6','7','8','9'}; String[]fontTypes={"Arial","ArialBlack","AvantGardeBkBT","Calibri"}; Randomrandom=newRandom(); StringBuilders=newStringBuilder(); for(inti=0;i<4;i++){ Stringr=String.valueOf(codeSeq[random.nextInt(codeSeq.length)]);//random.nextInt(10)); g.setColor(newColor(50+random.nextInt(100),50+random.nextInt(100),50+random.nextInt(100))); g.setFont(newFont(fontTypes[random.nextInt(fontTypes.length)],Font.BOLD,26)); g.drawString(r,15*i+5,19+random.nextInt(8)); // g.drawString(r,i*w/4,h-5); s.append(r); } returns.toString(); } }

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册