`
xiamizy
  • 浏览: 86876 次
  • 性别: Icon_minigender_1
  • 来自: 南京
博客专栏
78437efc-ad8e-387c-847f-a092d52e81a6
spring framew...
浏览量:4799
社区版块
存档分类
最新评论

struts2生成中文验证码的Action

 
阅读更多
struts2 的Action类 ValidateImgAction.java

java 代码
  1. packagecn.gx80.control.action.basic;
  2. importjava.io.ByteArrayInputStream;
  3. importjava.io.ByteArrayOutputStream;
  4. importjava.io.IOException;
  5. importjava.util.Map;
  6. importorg.apache.struts2.interceptor.SessionAware;
  7. importcn.gx80.service.util.ValidateImageService;
  8. importcn.gx80.util.Key;
  9. /**
  10. *生成验证码
  11. *@author飞天色鼠
  12. *
  13. */
  14. @SuppressWarnings("unchecked")
  15. publicclassValidateImgActionextendsStreamBasicActionimplementsSessionAware{
  16. privatestaticfinallongserialVersionUID=6894525175454169331L;
  17. privatestaticfinalStringDefault_ValidateCode="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  18. privateMapsession;
  19. privateintwidth;
  20. privateintheight;
  21. privateintfontSize;
  22. privateintcodeLength;
  23. privateValidateImageServicevalidateImageService;
  24. publicValidateImgAction(){
  25. this.contentType="image/jpeg";
  26. width=80;
  27. height=20;
  28. fontSize=16;
  29. codeLength=4;
  30. }
  31. publicvoidsetSession(Mapsession){
  32. this.session=session;
  33. }
  34. publicvoidsetWidth(intwidth){
  35. this.width=width;
  36. }
  37. publicvoidsetHeight(intheight){
  38. this.height=height;
  39. }
  40. publicvoidsetFontSize(intfontSize){
  41. this.fontSize=fontSize;
  42. }
  43. publicvoidsetCodeLength(intcodeLength){
  44. this.codeLength=codeLength;
  45. }
  46. publicvoidsetValidateImageService(ValidateImageServicevalidateImageService){
  47. this.validateImageService=validateImageService;
  48. }
  49. publicStringexecute()throwsException{
  50. session.put(Key.ValidateCodeByLogin,createInputStream(ValidateImageService.Disturb_Type_Simple));
  51. returnSUCCESS;
  52. }
  53. privateStringcreateInputStream(intdisturbType)throwsIOException{
  54. ByteArrayOutputStreambos=newByteArrayOutputStream();
  55. StringvalidateCode=null;
  56. validateCode=validateImageService.createValidateCode(disturbType,fontSize,bos,width,height,getText("System.validateCode",Default_ValidateCode),codeLength);
  57. inputStream=newByteArrayInputStream(bos.toByteArray());
  58. bos.close();
  59. returnvalidateCode;
  60. }
  61. }


验证码生成接口 ValidateImageService.java

java 代码
  1. packagecn.gx80.service.util;
  2. importjava.io.ByteArrayOutputStream;
  3. /**
  4. *验证码生成服务类
  5. *@author飞天色鼠
  6. *
  7. */
  8. publicinterfaceValidateImageService{
  9. /**
  10. *默认验证字符串
  11. */
  12. StringDefault_ValidateCode="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  13. /**
  14. *默认绘制干扰线的类型(不绘制干扰线)
  15. */
  16. intDisturb_Type_Default=0;
  17. /**
  18. *绘制单一色调的干扰线
  19. */
  20. intDisturb_Type_Simple=1;
  21. /**
  22. *绘制复杂的干扰线
  23. */
  24. intDisturb_Type_Complex=2;
  25. /**
  26. *生成验证图片并返回验证码
  27. *@paramdisturbType
  28. *@paramfontSize
  29. *@parambos
  30. *@paramwidth
  31. *@paramheight
  32. *@paramvalidateCode
  33. *@paramcodeLength
  34. *@return
  35. */
  36. publicabstractStringcreateValidateCode(intdisturbType,intfontSize,ByteArrayOutputStreambos,intwidth,intheight,StringvalidateCode,intcodeLength);
  37. }


验证码生成的实现 ValidateImageServiceImp.java

java 代码
  1. packagecn.gx80.service.util;
  2. importjava.awt.Color;
  3. importjava.awt.Font;
  4. importjava.awt.Graphics;
  5. importjava.awt.image.BufferedImage;
  6. importjava.io.ByteArrayOutputStream;
  7. importjava.io.IOException;
  8. importjava.util.Random;
  9. importjavax.imageio.ImageIO;
  10. importjavax.imageio.stream.ImageOutputStream;
  11. publicclassValidateImageServiceImpimplementsValidateImageService{
  12. publicStringcreateValidateCode(intdisturbType,intfontSize,ByteArrayOutputStreambos,intwidth,intheight,StringvalidateCode,intcodeLength){
  13. BufferedImagebImg=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
  14. Graphicsg=bImg.getGraphics();
  15. Randomrandom=newRandom();
  16. if(null==validateCode||validateCode.isEmpty()){
  17. validateCode=Default_ValidateCode;
  18. }
  19. if(fontSize>=height){
  20. fontSize=height-1;
  21. }
  22. drawOutline(g,width,height);
  23. switch(disturbType){
  24. caseDisturb_Type_Simple:
  25. drawSimpleDisturb(g,random,width,height);
  26. break;
  27. caseDisturb_Type_Complex:
  28. drawDisturb(g,random,width,height);
  29. break;
  30. default:
  31. break;
  32. }
  33. Stringcode=drawCode(g,random,validateCode,codeLength,width,height,fontSize);
  34. g.dispose();
  35. try{
  36. ImageOutputStreamimOut=ImageIO.createImageOutputStream(bos);
  37. ImageIO.write(bImg,"JPEG",imOut);
  38. imOut.close();
  39. }catch(IOExceptione){
  40. e.printStackTrace();
  41. }
  42. returncode;
  43. }
  44. /**
  45. *绘制边框
  46. *@paramg
  47. *@paramwidth
  48. *@paramheight
  49. */
  50. privatestaticvoiddrawOutline(Graphicsg,intwidth,intheight){
  51. g.setColor(Color.white);
  52. g.fillRect(0,0,width,height);
  53. g.setColor(Color.BLACK);
  54. g.drawRect(0,0,width-1,height-1);
  55. }
  56. /**
  57. *绘制比较复杂的干扰线
  58. *@paramg
  59. *@paramrandom
  60. *@paramwidth
  61. *@paramheight
  62. */
  63. privatestaticvoiddrawDisturb(Graphicsg,Randomrandom,intwidth,intheight){
  64. intx,y,x1,y1;
  65. for(inti=0;i<width;i++){
  66. x=random.nextInt(width);
  67. y=random.nextInt(height);
  68. x1=random.nextInt(12);
  69. y1=random.nextInt(12);
  70. g.setColor(getRandomColor(random,120,255));
  71. g.drawLine(x,y,x+x1,y+y1);
  72. g.fillArc(x,y,x1,y1,random.nextInt(360),random.nextInt(360));
  73. }
  74. }
  75. /**
  76. *绘制简单的干扰线
  77. *@paramg
  78. *@paramrandom
  79. *@paramwidth
  80. *@paramheight
  81. */
  82. privatestaticvoiddrawSimpleDisturb(Graphicsg,Randomrandom,intwidth,intheight){
  83. g.setColor(getRandomColor(random,160,200));
  84. for(inti=0;i<155;i++)
  85. {
  86. intx=random.nextInt(width);
  87. inty=random.nextInt(height);
  88. intxl=random.nextInt(12);
  89. intyl=random.nextInt(12);
  90. g.drawLine(x,y,x+xl,y+yl);
  91. }
  92. }
  93. /**
  94. *取得随机颜色
  95. *@paramrandom
  96. *@parampMin
  97. *@parampMax
  98. *@return
  99. */
  100. privatestaticColorgetRandomColor(Randomrandom,intpMin,intpMax){
  101. pMax=(Math.abs(pMax)>255?255:Math.abs(pMax));
  102. pMin=(Math.abs(pMin)>255?255:Math.abs(pMin));
  103. intr=pMin+random.nextInt(Math.abs(pMax-pMin));
  104. intg=pMin+random.nextInt(Math.abs(pMax-pMin));
  105. intb=pMin+random.nextInt(Math.abs(pMax-pMin));
  106. returnnewColor(r,g,b);
  107. }
  108. /**
  109. *绘制验证码
  110. *@paramg
  111. *@paramrandom
  112. *@paramvalidateCode
  113. *@paramcodeLength
  114. *@paramwidth
  115. *@paramheight
  116. *@paramfontSize
  117. *@return
  118. */
  119. privatestaticStringdrawCode(Graphicsg,Randomrandom,StringvalidateCode,intcodeLength,intwidth,intheight,intfontSize){
  120. intvalidateCodeLength=validateCode.length();
  121. Fontfont1=newFont("Verdana",Font.BOLD,fontSize);
  122. Fontfont2=newFont("serif",Font.BOLD,fontSize);
  123. StringBuffersb=newStringBuffer();
  124. intx,y;
  125. for(inti=0;i<codeLength;i++){
  126. x=(width/codeLength-1)*i+random.nextInt(width/(codeLength*2));
  127. y=random.nextInt(height-fontSize)+fontSize;
  128. sb.append(getRandomChar(validateCode,validateCodeLength,random));
  129. g.setColor(getRandomColor(random,70,150));
  130. if(sb.substring(i).getBytes().length>1)
  131. g.setFont(font2);
  132. else
  133. g.setFont(font1);
  134. g.drawString(sb.substring(i),x,y);
  135. }
  136. returnsb.toString();
  137. }
  138. /**
  139. *取得随机字符
  140. *@paramvalidateCode
  141. *@paramvalidateCodeLength
  142. *@paramrandom
  143. *@return
  144. */
  145. privatestaticchargetRandomChar(StringvalidateCode,intvalidateCodeLength,Randomrandom){
  146. returnvalidateCode.charAt(random.nextInt(validateCodeLength));
  147. }
  148. }

struts.xml 配置

xml 代码
  1. <packagename="gx80-test"namespace="/test"extends="gx80-default">
  2. <actionname="validateCode"class="cn.gx80.control.action.basic.ValidateImgAction">
  3. <interceptor-refname="gx80DefaultStack"/>
  4. <interceptor-refname="staticParams"/>
  5. <paramname="width">100</param>
  6. <paramname="height">30</param>
  7. <paramname="fontSize">18</param>
  8. <paramname="codeLength">5</param>
  9. <resultname="success"type="stream">
  10. <paramname="contentType">image/jpeg</param>
  11. </result>
  12. </action>
  13. </package>


当然还有语言文件中的验证码字符串 global.properties

java 代码
  1. System.validateCode=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/u4E00/u4E8C/u4E09/u56DB/u4E94/u516D/u4E03/u516B/u4E5D/u5341/u767E/u5343/u4E07/u5E7F/u897F/u5357/u5B81/u79D1/u56ED/u5927/u9053/u8F6F/u4EF6/u4E2D/u56FD/u4EBA/u6C11/u4E07/u5C81


一切配置好后,访问 127.0.0.1:8080/gx80/test/validateCode.do
分享到:
评论

相关推荐

    struts2 生成验证码

    在struts2下产生验证码,包含所需要的action(返回的就是验证码图片),struts配置文件,页面的调用。

    struts2实现验证码

    主要是用strus2实现验证码功能,里面包含验证码的实现以及xml文件里面的action的书写

    struts生成验证码

    使用struts的流Action响应生成一个验证码返回给客户端。 可以练习struts的使用,欢迎各位下载交流。

    汉字验证码(Servlet形式)

    生成验证码,本验证码为Servlet形式,可以凭开发经验,例如在Struts中,将其写在Action的方法中(void类型)从而转为Struts形式,调用时只需使img标签的src属性= 1.(servletname) 2.(methodname_actionname.action)

    Strut2版使用kaptcha验证码

    kaptcha验证码在使用上比其它...其中主要有几处配置,一是在Web.xml文件里增加相应的配置,后是在Action里做相关的生成及校验工作。前台采用不刷新的改变验证码,另外还做了一个手动获取验证码的功能,希望对大家有用。

    数字验证码(Servlet形式)

    生成验证码,本验证码为Servlet形式,可以凭开发经验,例如在Struts中,将其写在Action的方法中(void类型)从而转为Struts形式,调用时只需使img标签的src属性= 1.(servletname) 2.(methodname_actionname.action)

    达内Struts2.0学习之当当网系统学习案例

    Struts 2.0技术综合应用,包括上传图片功能,明文加密算法SHA-1和MD5,上传用户头像,根据action随机生成验证码,用链接实现数据的分页处理,以及拦截器和Logger日志框架的引入,总之相当强大,学习Struts 2.0,把这...

    jsp获取action传来的session和session清空以及判断.docx

    jsp获取action传来的session和session清空以及判断 jsp获取action传来的session是jsp开发中的一种常见需求,Session是Web应用...* 实现生成登录验证码 等等。Session的使用可以提高jsp应用程序的灵活性和可扩展性。

    当当网全套源码(附带邮箱验证功能)

    控制层:Struts2控制器组件、Action组件 c.业务层:Bean组件 d.数据访问层:DAO组件(JDBC) 4.数据库设计 1)数据库导入 create database dangdang; //创建库 use dangdang; //进入dangdang库 set names utf8;...

    Struts 成功测试

    \web\WebRoot\index.jsp 首页 =================================================================== D:\web\WebRoot\include\CSS.CSS D:\web\WebRoot\include\image.jsp 生成验证码的jsp ============...

    JAVA程序开发大全---上半部分

    20.5.4 生成随机验证码的imgNum类 358 20.5.5 用户登录页面index.jsp 359 20.5.6 验证用户登录信息的Servlet类login 360 20.6 显示宠物信息模块的实现 363 20.6.1 对应宠物的实体类User 363 20.6.2 定义对宠物信息...

    达内Java培训项目(当当网/通用电子商务系统)

    技术实现:Struts2、JSP、MySQL、Jquery、Javascript、Ajax 、Json、JUnit 项目描述: 本项目主要实现了用户管理模块、商品分类展示模块、购物车模块、订单模块。 用户管理模块实现了用户注册、实时验证、用户登录...

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

    iuhyiuhkjh908u0980

    本章我们将会开发基于Struts2框架的Hello World.我们的Hello World程序是你开发基于Struts2框架程序的第一步.这里我们会提供循序渐进的教程来开发基于Struts2框架的Hello World程序. 教程包含基本的步骤像创建目录...

    xmljava系统源码-javaweb1024-cluster:javaweb1024-集群

    Kaptcha(Google验证码) Druid(数据源) Atomikos(数据源事物) SpringCache(缓存集成redis) Shrio(权限控制,可以精确到按钮级别,集成redis) Redis(缓存服务器) Log4j Maven Generator(Mybatis自动生成工具) 前端技术点...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

     国内知名的高端IT技术作家,已出版《Spring 2.0宝典》、《基于J2EE的Ajax宝典》、《轻量级J2EE企业应用实战》、《Struts 2权威指南》、《Ruby On Rails敏捷开发最佳实践》等著作。 目录: 第0章 学习Java...

Global site tag (gtag.js) - Google Analytics