xhchc

危波帆墙,笑谈只在桃花上;与谁共尚,风吹万里浪; 相依相偎,不做黄泉想;莫惆怅,碧波潮生,一萧自狂放……

 

Struts2实现的6位数字的验证码程序

1、login.jsp页面程序

Java代码

 
  1. <script type="text/javascript">    
  2. function changeValidateCode(obj) {    
  3. //获取当前的时间作为参数,无具体意义    
  4. var timenow = new Date().getTime();    
  5. //每次请求需要一个不同的参数,否则可能会返回同样的验证码    
  6. //这和浏览器的缓存机制有关系,也可以把页面设置为不缓存,这样就不用这个参数了。    
  7. obj.src="rand.action?d="+timenow;    
  8. }    
  9. </script>   
  10.   
  11. 在表单中添加下面这句话:   
  12. <s:text name="random"></s:text>:<s:textfield name="rand" size="5"></s:textfield><img src="rand.action" title="点击图片刷新验证码"/>  

2、RandomNumUtil.java 生成验证码的类文件

Java代码
  1. import java.awt.Color;   
  2. import java.awt.Font;   
  3. import java.awt.Graphics;   
  4. import java.awt.image.BufferedImage;   
  5. import java.io.ByteArrayInputStream;   
  6. import java.io.ByteArrayOutputStream;   
  7. import java.util.Random;   
  8. import javax.imageio.ImageIO;   
  9. import javax.imageio.stream.ImageOutputStream;   
  10. public class RandomNumUtil {    
  11. private ByteArrayInputStream image;//图像    
  12. private String str;//验证码    
  13.   
  14. private RandomNumUtil(){    
  15. init();//初始化属性    
  16. }    
  17. /*   
  18. * 取得RandomNumUtil实例   
  19. */    
  20. public static RandomNumUtil Instance(){    
  21. return new RandomNumUtil();    
  22. }    
  23. /*   
  24. * 取得验证码图片   
  25. */    
  26. public ByteArrayInputStream getImage(){    
  27. return this.image;    
  28. }    
  29. /*   
  30. * 取得图片的验证码   
  31. */    
  32. public String getString(){    
  33. return this.str;    
  34. }    
  35.   
  36. private void init() {    
  37. // 在内存中创建图象    
  38. int width=85, height=20;    
  39. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);    
  40. // 获取图形上下文    
  41. Graphics g = image.getGraphics();    
  42. // 生成随机类    
  43. Random random = new Random();    
  44. // 设定背景色    
  45. g.setColor(getRandColor(200,250));    
  46. g.fillRect(00, width, height);    
  47. // 设定字体    
  48. g.setFont(new Font("Times New Roman",Font.PLAIN,18));    
  49. // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到    
  50. g.setColor(getRandColor(160,200));    
  51. for (int i=0;i<155;i++)    
  52. {    
  53. int x = random.nextInt(width);    
  54. int y = random.nextInt(height);    
  55. int xl = random.nextInt(12);    
  56. int yl = random.nextInt(12);    
  57. g.drawLine(x,y,x+xl,y+yl);    
  58. }    
  59. // 取随机产生的认证码(6位数字)    
  60. String sRand="";    
  61. for (int i=0;i<6;i++){    
  62. String rand=String.valueOf(random.nextInt(10));    
  63. sRand+=rand;    
  64. // 将认证码显示到图象中    
  65. g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));    
  66. // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成    
  67. g.drawString(rand,13*i+6,16);    
  68. }   
  69. //赋值验证码   
  70. this.str=sRand;    
  71.   
  72. //图象生效    
  73. g.dispose();    
  74. ByteArrayInputStream input=null;    
  75. ByteArrayOutputStream output = new ByteArrayOutputStream();    
  76. try{    
  77. ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);    
  78. ImageIO.write(image, "JPEG", imageOut);    
  79. imageOut.close();    
  80. input = new ByteArrayInputStream(output.toByteArray());    
  81. }catch(Exception e){    
  82. System.out.println("验证码图片产生出现错误:"+e.toString());    
  83. }    
  84.   
  85. this.image=input;/* 赋值图像 */    
  86. }    
  87. /*   
  88. * 给定范围获得随机颜色   
  89. */    
  90. private Color getRandColor(int fc,int bc){    
  91. Random random = new Random();    
  92. if(fc>255) fc=255;    
  93. if(bc>255) bc=255;    
  94. int r=fc+random.nextInt(bc-fc);    
  95. int g=fc+random.nextInt(bc-fc);    
  96. int b=fc+random.nextInt(bc-fc);    
  97. return new Color(r,g,b);    
  98. }   
  99. }  


3、RandomAction.java  生成验证码的action程序

Java代码 复制代码
  1. import java.io.ByteArrayInputStream;   
  2. import com.mxl.util.RandomNumUtil;   
  3. import com.opensymphony.xwork2.ActionContext;   
  4. import com.opensymphony.xwork2.ActionSupport;   
  5. public class RandomAction extends ActionSupport{    
  6. private ByteArrayInputStream inputStream;    
  7. public String execute() throws Exception{    
  8. RandomNumUtil rdnu=RandomNumUtil.Instance();    
  9. this.setInputStream(rdnu.getImage());//取得带有随机字符串的图片    
  10. ActionContext.getContext().getSession().put("random", rdnu.getString());//取得随机字符串放入HttpSession    
  11. return SUCCESS;    
  12. }    
  13. public void setInputStream(ByteArrayInputStream inputStream) {    
  14. this.inputStream = inputStream;    
  15. }    
  16. public ByteArrayInputStream getInputStream() {    
  17. return inputStream;    
  18. }   
  19. }  


4、LoginAction.java 验证验证码的action

Java代码 复制代码
  1. private String rand; //表单中的rand   
  2. public String getRand() {   
  3. return rand;   
  4. }   
  5. public void setRand(String rand) {   
  6. this.rand = rand;   
  7. }   
  8. //从session中取出RandomAction.java 中生成的验证码random   
  9. String arandom=(String)(ActionContext.getContext().getSession().get("random"));   
  10.   
  11. //下面就是将session中保存验证码字符串与客户输入的验证码字符串对比了   
  12. if(arandom.equals(this.getRand())) {   
  13. ActionContext.getContext().getSession().put("user"this.getUsername());   
  14. return SUCCESS;   
  15. }   
  16. else {   
  17. return ERROR;   
  18. }  

5、配置struts.xml文件

Java代码 复制代码
  1. <!-- Random验证码 -->   
  2. <action name="rand" class="com.mxl.rand.RandomAction">      
  3.        <result type="stream">      
  4.             <param name="contentType">image/jpeg</param>      
  5.             <param name="inputName">inputStream</param>      
  6.        </result>   
  7.    </action>  

6、生成的验证码图片演示(实现的6位数字的验证码)



说明:

如果想修改验证码生成的个数,需要修改以下几个地方:

第一点:
Java代码 复制代码
  1. int width=85, height=20;  


第二点:
Java代码 复制代码
  1. for (int i=0;i<6;i++)  


数字6,修改成你想生成的位数就可以了~

posted on 2009-03-04 17:50 chu 阅读(1601) 评论(1)  编辑  收藏

评论

# re: Struts2实现的6位数字的验证码程序 2009-03-09 20:41 CoderDream

楼主不厚道,转帖既不说明,也不给出原文链接!

原文地址:http://www.javaeye.com/topic/300128  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(2)

随笔档案

我的链接

搜索

最新评论

阅读排行榜

评论排行榜