小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Struts2實現(xiàn)的6位數(shù)字的驗證碼程序

 feimishiwo 2014-08-13
1、login.jsp頁面程序

Java代碼  收藏代碼
  1. <script type="text/javascript">   
  2. function changeValidateCode(obj) {   
  3. //獲取當前的時間作為參數(shù),無具體意義   
  4. var timenow = new Date().getTime();   
  5. //每次請求需要一個不同的參數(shù),否則可能會返回同樣的驗證碼   
  6. //這和瀏覽器的緩存機制有關系,也可以把頁面設置為不緩存,這樣就不用這個參數(shù)了。   
  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"  onclick="changeValidateCode(this)" 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. // 在內(nèi)存中創(chuàng)建圖象   
  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(0, 0, width, height);   
  47. // 設定字體   
  48. g.setFont(new Font("Times New Roman",Font.PLAIN,18));   
  49. // 隨機產(chǎn)生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. // 取隨機產(chǎn)生的認證碼(6位數(shù)字)   
  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. // 調(diào)用函數(shù)出來的顏色相同,可能是因為種子太接近,所以只能直接生成   
  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("驗證碼圖片產(chǎn)生出現(xiàn)錯誤:"+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、生成的驗證碼圖片演示(實現(xiàn)的6位數(shù)字的驗證碼)



說明:

如果想修改驗證碼生成的個數(shù),需要修改以下幾個地方:

第一點:
Java代碼  收藏代碼
  1. int width=85, height=20;  


第二點:
Java代碼  收藏代碼
  1. for (int i=0;i<6;i++)  


數(shù)字6,修改成你想生成的位數(shù)就可以了~

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約