歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> jQuery+Struts2無刷新驗證碼

jQuery+Struts2無刷新驗證碼

日期:2017/3/1 10:41:19   编辑:Linux編程

1.產生圖片的工廠:

  1. package org.blog.util;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.image.BufferedImage;
  6. import java.io.ByteArrayInputStream;
  7. import java.io.ByteArrayOutputStream;
  8. import java.util.Random;
  9. import javax.imageio.ImageIO;
  10. import javax.imageio.stream.ImageOutputStream;
  11. public class RandomNumUtil
  12. {
  13. private ByteArrayInputStream image;
  14. private String str;
  15. public RandomNumUtil()
  16. {
  17. init();
  18. }
  19. public static RandomNumUtil Instance()
  20. {
  21. return new RandomNumUtil();
  22. }
  23. public ByteArrayInputStream getImage()
  24. {
  25. return this.image;
  26. }
  27. public String getString()
  28. {
  29. return this.str;
  30. }
  31. private void init()
  32. {
  33. //在內存中創建圖象
  34. int width = 109;
  35. int height = 40;
  36. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  37. /* 獲取內在中圖像的上下文 */
  38. Graphics g = image.getGraphics();
  39. /* 創建一個隨機類 */
  40. Random random = new Random();
  41. /* 設置背景顏色 */
  42. g.setColor(this.getRandColor(200, 250));
  43. g.fillRect(0, 0, width, height);
  44. /* 設置字體 */
  45. g.setFont(new Font("Times New Roman", Font.PLAIN, 28));
  46. /* 設置干擾線的顏色 */
  47. g.setColor(getRandColor(160, 200));
  48. for (int i=0; i<155; i++)
  49. {
  50. int x = random.nextInt(width);
  51. int y = random.nextInt(height);
  52. int xl = random.nextInt(12);
  53. int yl = random.nextInt(12);
  54. g.drawLine(x, y, x+xl, y+yl);
  55. }
  56. /* 用來臨時保存隨機產生的數字 */
  57. String sRand = "";
  58. for (int i=0; i<4; i++)
  59. {
  60. String rand = String.valueOf(random.nextInt(10));
  61. sRand += rand;
  62. g.setColor(new Color(20 + random.nextInt(110), 20+random.nextInt(110), 20+random.nextInt(110) ));
  63. g.drawString(rand, 20*i+10, 25);
  64. /* 然後賦給str */
  65. this.str = sRand;
  66. }
  67. /* 使圖像生效 */
  68. g.dispose();
  69. ByteArrayInputStream input = null;
  70. ByteArrayOutputStream output = new ByteArrayOutputStream();
  71. try
  72. {
  73. ImageOutputStream imageOutput = ImageIO.createImageOutputStream(output);
  74. ImageIO.write(image, "JPEG", imageOutput);
  75. imageOutput.close();
  76. input = new ByteArrayInputStream(output.toByteArray());
  77. } catch (Exception e)
  78. {
  79. System.out.println("驗證碼圖片產生出現錯誤:"+e.toString());
  80. }
  81. this.image = input;
  82. }
  83. /* 隨機產生顏色 */
  84. private Color getRandColor(int fc, int bc)
  85. {
  86. Random random = new Random();
  87. if (fc > 255)
  88. fc = 255;
  89. if (bc > 255)
  90. bc = 255;
  91. int r = fc + random.nextInt(bc - fc);
  92. int g = fc + random.nextInt(bc - fc);
  93. int b = fc + random.nextInt(bc - fc);
  94. return new Color(r, g, b);
  95. }
  96. }

2.通過Action獲取該圖片:

  1. package org.blog.admin.action;
  2. import java.io.ByteArrayInputStream;
  3. import org.blog.util.RandomNumUtil;
  4. import com.opensymphony.xwork2.ActionContext;
  5. import com.opensymphony.xwork2.ActionSupport;
  6. public class GetRandomNum extends ActionSupport
  7. {
  8. private static final long serialVersionUID = 1L;
  9. private ByteArrayInputStream inputStream;
  10. public ByteArrayInputStream getInputStream()
  11. {
  12. return inputStream;
  13. }
  14. public void setInputStream(ByteArrayInputStream inputStream)
  15. {
  16. this.inputStream = inputStream;
  17. }
  18. public String execute() throws Exception
  19. {
  20. RandomNumUtil randomNumUtil = RandomNumUtil.Instance();
  21. this.setInputStream(randomNumUtil.getImage());
  22. ActionContext.getContext().getSession().put("validateCode", randomNumUtil.getString());
  23. return SUCCESS;
  24. }
  25. }

3.在strus.xml中配置:

  1. <!-- 獲取隨機產生的數字驗證碼 -->
  2. <action name="getRandomNumber" class="org.blog.admin.action.GetRandomNum">
  3. <result type="stream">
  4. <param name="contentType">image/jpeg</param>
  5. <param name="inputName">inputStream</param>
  6. </result>
  7. </action>

4.在jsp文件中:

  1. <img src="getRandomNumber" width="90" height="25" alt="驗證碼圖片" id="randomCode"/>
  2. <a href="#" id="refresh" style="font-size:12px;">看不楚,換張圖片</a>

5.局部刷新驗證碼的js代碼:(主要的思想是讓圖片的src屬性,通過js來每次重新加載一次action.可以通過產生隨機數或以當前時間為種子,這樣每次都去重新讀取產生圖片的Action了)

  1. $(function() {
  2. $("#refresh").click(function() {
  3. var rom = new Date();
  4. $("#randomCode").attr("src", "getRandomNumber?timestamp="+rom);
  5. });
  6. });
Copyright © Linux教程網 All Rights Reserved