jQuery+Struts2无刷新验证码


1.产生图片的工厂:

  1. package org.blog.util;  
  2.   
  3.   
  4. import java.awt.Color;  
  5. import java.awt.Font;  
  6. import java.awt.Graphics;  
  7. import java.awt.image.BufferedImage;  
  8. import java.io.ByteArrayInputStream;  
  9. import java.io.ByteArrayOutputStream;  
  10. import java.util.Random;  
  11.   
  12.   
  13. import javax.imageio.ImageIO;  
  14. import javax.imageio.stream.ImageOutputStream;  
  15.   
  16.   
  17.   
  18.   
  19. public class RandomNumUtil  
  20. {  
  21.     private ByteArrayInputStream    image;  
  22.     private String                  str;  
  23.       
  24.     public RandomNumUtil()  
  25.     {  
  26.         init();  
  27.     }  
  28.       
  29.     public static RandomNumUtil Instance()  
  30.     {  
  31.         return new RandomNumUtil();  
  32.     }  
  33.       
  34.     public ByteArrayInputStream getImage()  
  35.     {  
  36.         return this.image;  
  37.     }  
  38.       
  39.     public String getString()  
  40.     {  
  41.         return this.str;  
  42.     }  
  43.       
  44.     private void init()  
  45.     {  
  46.         //在内存中创建图象     
  47.         int width = 109;  
  48.         int height = 40;  
  49.         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  50.           
  51.         /* 获取内在中图像的上下文 */  
  52.         Graphics g = image.getGraphics();  
  53.           
  54.         /* 创建一个随机类 */  
  55.         Random random = new Random();  
  56.           
  57.         /* 设置背景颜色 */  
  58.         g.setColor(this.getRandColor(200250));  
  59.         g.fillRect(00, width, height);  
  60.           
  61.         /* 设置字体 */  
  62.         g.setFont(new Font("Times New Roman", Font.PLAIN, 28));  
  63.           
  64.         /* 设置干扰线的颜色 */  
  65.         g.setColor(getRandColor(160200));  
  66.         for (int i=0; i<155; i++)  
  67.         {  
  68.             int x = random.nextInt(width);  
  69.             int y = random.nextInt(height);  
  70.             int xl = random.nextInt(12);  
  71.             int yl = random.nextInt(12);  
  72.             g.drawLine(x, y, x+xl, y+yl);  
  73.         }  
  74.           
  75.         /* 用来临时保存随机产生的数字 */  
  76.         String sRand = "";  
  77.         for (int i=0; i<4; i++)  
  78.         {  
  79.             String rand = String.valueOf(random.nextInt(10));  
  80.             sRand += rand;  
  81.             g.setColor(new Color(20 + random.nextInt(110), 20+random.nextInt(110), 20+random.nextInt(110) ));  
  82.             g.drawString(rand, 20*i+1025);  
  83.             /* 然后赋给str */  
  84.             this.str = sRand;  
  85.         }  
  86.         /* 使图像生效 */  
  87.         g.dispose();  
  88.           
  89.           
  90.         ByteArrayInputStream input = null;  
  91.         ByteArrayOutputStream output = new ByteArrayOutputStream();  
  92.         try  
  93.         {  
  94.             ImageOutputStream imageOutput = ImageIO.createImageOutputStream(output);  
  95.             ImageIO.write(image, "JPEG", imageOutput);  
  96.             imageOutput.close();  
  97.             input = new ByteArrayInputStream(output.toByteArray());  
  98.         } catch (Exception e)  
  99.         {  
  100.             System.out.println("验证码图片产生出现错误:"+e.toString());    
  101.         }  
  102.         this.image = input;  
  103.     }  
  104.       
  105.     /* 随机产生颜色 */  
  106.     private Color getRandColor(int fc, int bc)  
  107.     {  
  108.         Random random = new Random();  
  109.         if (fc > 255)  
  110.             fc = 255;  
  111.         if (bc > 255)  
  112.             bc = 255;  
  113.         int r = fc + random.nextInt(bc - fc);  
  114.         int g = fc + random.nextInt(bc - fc);  
  115.         int b = fc + random.nextInt(bc - fc);  
  116.         return new Color(r, g, b);  
  117.     }  
  118. }  

2.通过Action获取该图片:

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

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. });  

相关内容