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 閱讀(1621) 評論(1)  編輯  收藏

          評論

          # re: Struts2實現的6位數字的驗證碼程序 2009-03-09 20:41 CoderDream

          樓主不厚道,轉帖既不說明,也不給出原文鏈接!

          原文地址:http://www.javaeye.com/topic/300128  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          我的鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 保靖县| 佛山市| 瓦房店市| 潜江市| 兴文县| 彩票| 大渡口区| 林周县| 荔波县| 喀什市| 宜兴市| 柳河县| 高雄县| 马关县| 凤台县| 临猗县| 信宜市| 常德市| 广昌县| 嘉善县| 周至县| 太湖县| 大关县| 东阳市| 商丘市| 扎鲁特旗| 祥云县| 兴城市| 怀远县| 东乡| 临泉县| 三穗县| 松潘县| 同德县| 尖扎县| 德江县| 秭归县| 通州市| 图木舒克市| 濮阳县| 金堂县|