strut2图形验证码

[java]  view plain copy
  1. import com.opensymphony.xwork2.ActionSupport;  
  2.   
  3. import java.util.Random;  
  4. import java.awt.*;  
  5. import java.awt.image.*;  
  6. import java.io.ByteArrayOutputStream;  
  7.   
  8. import org.apache.struts2.ServletActionContext;  
  9.   
  10. public class ImageAction extends ActionSupport {  
  11.   
  12.     /** 
  13.      *  
  14.      */  
  15.     private static final long serialVersionUID = 1L;  
  16.   
  17.     /** 
  18.      * 验证码对应的Session名 
  19.      */  
  20.   
  21.     private static final String SESSIONNAME = "CheckCodeImageAction";  
  22.   
  23.     /** 
  24.      * 用于随机生成验证码的数据源 
  25.      */  
  26.     private static final char[] SOURCECODE = new char[]{  
  27.        'a''b''c''d''e''f''g''h''i''j',  
  28.        'k''l''m''n''o''p''q''r''s''t',   
  29.        'u''v''w''x''y''z',  
  30.        'A''B''C''D''E''F''G''H''I''J',  
  31.        'K''L''M''N''O''P''Q''R''S''T',   
  32.        'U''V''W''X''Y''Z',  
  33.        '1''2''3''4''5''6''7''8''9''0'  
  34.     };  
  35.     /** 
  36.      * 用于随机打印验证码的字符颜色 
  37.      */  
  38.     private static final Color[] COLORS = new Color[]{  
  39.        Color.RED, Color.BLUE, Color.BLACK, Color.GREEN, Color.YELLOW, Color.CYAN  
  40.     };  
  41.     /** 
  42.      * 用于打印验证码的字体 
  43.      */  
  44.     private static final Font FONTCODE = new Font("Times New Roman", Font.ITALIC, 25);  
  45.     /** 
  46.      * 用于生成随机数的随机数生成器 
  47.      */  
  48.     private static final Random rdm = new Random();  
  49.     private String text = "";  
  50.     private byte[] bytes = null;  
  51.     private String contentType = "image/png";  
  52.   
  53.     public byte[] getImageBytes(){  
  54.        return this.bytes;  
  55.     }  
  56.   
  57.     public String getContentType(){  
  58.        return this.contentType;  
  59.     }  
  60.   
  61.     public void setContentType(String value){  
  62.        this.contentType = value;  
  63.     }  
  64.   
  65.     public int getContentLength(){  
  66.        return bytes.length;  
  67.     }  
  68.   
  69.     /** 
  70.      * 生成长度为4的随机字符串 
  71.      */  
  72.     private void generateText(){  
  73.        char[] source = new char[4];  
  74.        for(int i=0; i<source.length; i++){  
  75.            source[i] = ImageAction.SOURCECODE[rdm.nextInt(ImageAction.SOURCECODE.length)];  
  76.        }  
  77.        this.text = new String(source);  
  78.        // 设置Session  
  79.        ServletActionContext.getRequest().getSession().setAttribute(SESSIONNAME, this.text);  
  80.     }  
  81.   
  82.     /** 
  83.      * 在内存中生成打印了随机字符串的图片 
  84.      * @return 在内存中创建的打印了字符串的图片 
  85.      */  
  86.     private BufferedImage createImage(){  
  87.        int width = 150;  
  88.        int height = 45;  
  89.   
  90.        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  91.        Graphics g = image.getGraphics();  
  92.        g.setColor(Color.LIGHT_GRAY);  
  93.        g.fillRect(00, width, height);  
  94.        g.setFont(FONTCODE);  
  95.        for(int i=0; i<this.text.length(); i++){  
  96.            g.setColor(COLORS[rdm.nextInt(COLORS.length)]);  
  97.            g.drawString(this.text.substring(i, i+1), 9 + rdm.nextInt(i + 5) * 1145-rdm.nextInt(7)*3 );  
  98.        }  
  99.        g.dispose();  
  100.        return image;  
  101.     }  
  102.   
  103.     /** 
  104.      * 根据图片创建字节数组 
  105.      * @param image 用于创建字节数组的图片 
  106.      */  
  107.     private void generatorImageBytes(BufferedImage image){  
  108.        ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  109.        try{  
  110.            javax.imageio.ImageIO.write(image, "jpg", bos);  
  111.            this.bytes = bos.toByteArray();  
  112.        }catch(Exception ex){  
  113.            ex.printStackTrace();  
  114.        }finally{  
  115.            try{  
  116.               bos.close();  
  117.            }catch(Exception ex1){  
  118.                ex1.printStackTrace();  
  119.            }  
  120.        }  
  121.     }  
  122.   
  123.     /** 
  124.      * 被struts2过滤器调用的方法 
  125.      * @return 永远返回字符串"image" 
  126.      */  
  127.     public String getImage(){  
  128.        this.generateText();  
  129.        BufferedImage image = this.createImage();  
  130.        this.generatorImageBytes(image);  
  131.        return "image";  
  132.     }  
  133. }  
  134.   
  135. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
  136.   
  137. import com.opensymphony.xwork2.ActionInvocation;  
  138.   
  139. import com.opensymphony.xwork2.Result;  
  140. import com.rs.learnOnline.action.ImageAction;  
  141.   
  142. import javax.servlet.http.HttpServletResponse;  
  143.   
  144. import org.apache.struts2.ServletActionContext;  
  145.   
  146. public class ImageResult implements Result {  
  147.     /** 
  148.      *  
  149.      */  
  150.     private static final long serialVersionUID = 5205515981838231120L;  
  151.   
  152.     public void execute(ActionInvocation ai) throws Exception {  
  153.   
  154.        ImageAction action = (ImageAction)ai.getAction();  
  155.   
  156.        HttpServletResponse response = ServletActionContext.getResponse();  
  157.   
  158.        response.setHeader("Cash""no cash");  
  159.   
  160.        response.setContentType(action.getContentType());  
  161.   
  162.        response.setContentLength(action.getContentLength());  
  163.   
  164.        response.getOutputStream().write(action.getImageBytes());  
  165.   
  166.        response.getOutputStream().flush();  
  167.     }  
  168. }  
  169.   
  170. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
  171. <package namespace="/test" name="testPackage" extends="struts-default">  
  172.        <result-types>  
  173.            <result-type name="ValidateImage" class="com.rs.learnOnline.dao.impl.ImageResult" />  
  174.        </result-types>  
  175.        <action name="ValidateImage" class="com.rs.learnOnline.action.ImageAction" method="getImage">  
  176.            <result name="image" type="ValidateImage" />  
  177.        </action>  
  178.     </package>  
  179.   
  180. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
  181.   
  182. function changeValidateCode(obj) {  
  183.          //获取当前的时间作为参数,无具体意义  
  184.          var timenow = new Date().getTime();  
  185.          //每次请求需要一个不同的参数,否则可能会返回同样的验证码  
  186.          //可能和浏览器的缓存机制有关系  
  187.          obj.src="test/ValidateImage?d="+timenow;  
  188.      }  
  189.   
  190.   
  191. <td align="right">验证图片:</td>  
  192.     <td><img id="vfcode" alt="操作频繁,稍后再试" src="test/ValidateImage" οnclick="changeValidateCode(this)" title="看不清楚? 换张图片" />   
  193.     </td>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值