日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

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

 feimishiwo 2014-08-13
1、login.jsp頁(yè)面程序

Java代碼  收藏代碼
  1. <script type="text/javascript">   
  2. function changeValidateCode(obj) {   
  3. //獲取當(dāng)前的時(shí)間作為參數(shù),無(wú)具體意義   
  4. var timenow = new Date().getTime();   
  5. //每次請(qǐng)求需要一個(gè)不同的參數(shù),否則可能會(huì)返回同樣的驗(yàn)證碼   
  6. //這和瀏覽器的緩存機(jī)制有關(guān)系,也可以把頁(yè)面設(shè)置為不緩存,這樣就不用這個(gè)參數(shù)了。   
  7. obj.src="rand.action?d="+timenow;   
  8. }   
  9. </script>  
  10.   
  11. 在表單中添加下面這句話(huà):  
  12. <s:text name="random"></s:text>:<s:textfield name="rand" size="5"></s:textfield><img src="rand.action"  onclick="changeValidateCode(this)" title="點(diǎn)擊圖片刷新驗(yàn)證碼"/>  


2、RandomNumUtil.java 生成驗(yàn)證碼的類(lèi)文件

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;//驗(yàn)證碼   
  13.   
  14. private RandomNumUtil(){   
  15. init();//初始化屬性   
  16. }   
  17. /*  
  18. * 取得RandomNumUtil實(shí)例  
  19. */   
  20. public static RandomNumUtil Instance(){   
  21. return new RandomNumUtil();   
  22. }   
  23. /*  
  24. * 取得驗(yàn)證碼圖片  
  25. */   
  26. public ByteArrayInputStream getImage(){   
  27. return this.image;   
  28. }   
  29. /*  
  30. * 取得圖片的驗(yàn)證碼  
  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. // 生成隨機(jī)類(lèi)   
  43. Random random = new Random();   
  44. // 設(shè)定背景色   
  45. g.setColor(getRandColor(200,250));   
  46. g.fillRect(0, 0, width, height);   
  47. // 設(shè)定字體   
  48. g.setFont(new Font("Times New Roman",Font.PLAIN,18));   
  49. // 隨機(jī)產(chǎn)生155條干擾線(xiàn),使圖象中的認(rèn)證碼不易被其它程序探測(cè)到   
  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. // 取隨機(jī)產(chǎn)生的認(rè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. // 將認(rèn)證碼顯示到圖象中   
  65. g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));   
  66. // 調(diào)用函數(shù)出來(lái)的顏色相同,可能是因?yàn)榉N子太接近,所以只能直接生成   
  67. g.drawString(rand,13*i+6,16);   
  68. }  
  69. //賦值驗(yàn)證碼  
  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("驗(yàn)證碼圖片產(chǎn)生出現(xiàn)錯(cuò)誤:"+e.toString());   
  83. }   
  84.   
  85. this.image=input;/* 賦值圖像 */   
  86. }   
  87. /*  
  88. * 給定范圍獲得隨機(jī)顏色  
  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  生成驗(yàn)證碼的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());//取得帶有隨機(jī)字符串的圖片   
  10. ActionContext.getContext().getSession().put("random", rdnu.getString());//取得隨機(jī)字符串放入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 驗(yàn)證驗(yàn)證碼的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 中生成的驗(yàn)證碼random  
  9. String arandom=(String)(ActionContext.getContext().getSession().get("random"));  
  10.   
  11. //下面就是將session中保存驗(yàn)證碼字符串與客戶(hù)輸入的驗(yàn)證碼字符串對(duì)比了  
  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驗(yàn)證碼 -->  
  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、生成的驗(yàn)證碼圖片演示(實(shí)現(xiàn)的6位數(shù)字的驗(yàn)證碼)



說(shuō)明:

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

第一點(diǎn):
Java代碼  收藏代碼
  1. int width=85, height=20;  


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


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

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多