分享

struts2生成中文验证码的Action

 pengx 2008-12-01
struts2 的Action类 ValidateImgAction.java

java 代码
 
  1. package cn.gx80.control.action.basic;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.IOException;  
  6. import java.util.Map;  
  7.   
  8. import org.apache.struts2.interceptor.SessionAware;  
  9.   
  10. import cn.gx80.service.util.ValidateImageService;  
  11. import cn.gx80.util.Key;  
  12.   
  13. /** 
  14.  * 生成验证码 
  15.  * @author 飞天色鼠 
  16.  * 
  17.  */  
  18. @SuppressWarnings("unchecked")  
  19. public class ValidateImgAction extends StreamBasicAction implements SessionAware{  
  20.   
  21.     private static final long serialVersionUID = 6894525175454169331L;  
  22.   
  23.     private static final String Default_ValidateCode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
  24.       
  25.     private Map session;  
  26.       
  27.     private int width;  
  28.     private int height;  
  29.     private int fontSize;  
  30.     private int codeLength;  
  31.       
  32.     private ValidateImageService validateImageService;  
  33.       
  34.        
  35.     public ValidateImgAction(){  
  36.         this.contentType = "image/jpeg";      
  37.         width = 80;  
  38.         height = 20;  
  39.         fontSize = 16;  
  40.         codeLength = 4;  
  41.     }  
  42.         
  43.     public void setSession(Map session) {  
  44.         this.session = session;  
  45.     }   
  46.       
  47.       
  48.     public void setWidth(int width) {  
  49.         this.width = width;  
  50.     }  
  51.   
  52.     public void setHeight(int height) {  
  53.         this.height = height;  
  54.     }  
  55.   
  56.   
  57.     public void setFontSize(int fontSize) {  
  58.         this.fontSize = fontSize;  
  59.     }  
  60.   
  61.     public void setCodeLength(int codeLength) {  
  62.         this.codeLength = codeLength;  
  63.     }  
  64.   
  65.     public void setValidateImageService(ValidateImageService validateImageService) {  
  66.         this.validateImageService = validateImageService;  
  67.     }  
  68.   
  69.     public String execute() throws Exception {    
  70.         session.put(Key.ValidateCodeByLogin, createInputStream(ValidateImageService.Disturb_Type_Simple));  
  71.         return SUCCESS;  
  72.     }  
  73.       
  74.       
  75.     private String createInputStream(int disturbType) throws IOException{  
  76.         ByteArrayOutputStream bos =new ByteArrayOutputStream();  
  77.         String validateCode = null;  
  78.         validateCode = validateImageService.createValidateCode(disturbType,fontSize, bos, width, height, getText("System.validateCode",Default_ValidateCode), codeLength);  
  79.         inputStream = new ByteArrayInputStream(bos.toByteArray());  
  80.         bos.close();  
  81.         return validateCode;  
  82.     }                           
  83. }  


验证码生成接口 ValidateImageService.java

java 代码
 
  1. package cn.gx80.service.util;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4.   
  5. /** 
  6.  * 验证码生成服务类 
  7.  * @author 飞天色鼠 
  8.  * 
  9.  */  
  10. public interface ValidateImageService {  
  11.     /** 
  12.      * 默认验证字符串 
  13.      */  
  14.     String Default_ValidateCode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
  15.     /** 
  16.      * 默认绘制干扰线的类型(不绘制干扰线) 
  17.      */  
  18.     int Disturb_Type_Default = 0;  
  19.     /** 
  20.      * 绘制单一色调的干扰线 
  21.      */  
  22.     int Disturb_Type_Simple = 1;  
  23.     /** 
  24.      * 绘制复杂的干扰线 
  25.      */  
  26.     int Disturb_Type_Complex = 2;  
  27.       
  28.     /** 
  29.      * 生成验证图片并返回验证码 
  30.      * @param disturbType 
  31.      * @param fontSize 
  32.      * @param bos 
  33.      * @param width 
  34.      * @param height 
  35.      * @param validateCode 
  36.      * @param codeLength 
  37.      * @return 
  38.      */  
  39.     public abstract String createValidateCode(int disturbType, int fontSize, ByteArrayOutputStream bos, int width, int height, String validateCode,int codeLength);  
  40.       
  41. }  


验证码生成的实现 ValidateImageServiceImp.java

java 代码
 
  1. package cn.gx80.service.util;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics;  
  6. import java.awt.image.BufferedImage;  
  7. import java.io.ByteArrayOutputStream;  
  8. import java.io.IOException;  
  9. import java.util.Random;  
  10.   
  11. import javax.imageio.ImageIO;  
  12. import javax.imageio.stream.ImageOutputStream;  
  13.   
  14.   
  15. public class ValidateImageServiceImp implements ValidateImageService{  
  16.       
  17.     public String createValidateCode(int disturbType, int fontSize, ByteArrayOutputStream bos, int width, int height, String validateCode,int codeLength){  
  18.         BufferedImage bImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);  
  19.         Graphics g = bImg.getGraphics();  
  20.         Random random = new Random();  
  21.           
  22.         if(null == validateCode || validateCode.isEmpty()){  
  23.             validateCode = Default_ValidateCode;  
  24.         }  
  25.         if(fontSize >= height){  
  26.             fontSize = height-1;  
  27.         }  
  28.           
  29.         drawOutline(g,width,height);  
  30.         switch (disturbType) {  
  31.         case Disturb_Type_Simple:  
  32.             drawSimpleDisturb(g,random,width,height);  
  33.             break;  
  34.         case Disturb_Type_Complex:  
  35.             drawDisturb(g,random,width,height);  
  36.             break;  
  37.         default:  
  38.             break;  
  39.         }  
  40.           
  41.         String code = drawCode(g,random,validateCode,codeLength,width,height,fontSize);  
  42.         g.dispose();  
  43.         try {                
  44.             ImageOutputStream   imOut   =ImageIO.createImageOutputStream(bos);    
  45.             ImageIO.write(bImg,"JPEG",imOut);  
  46.             imOut.close();  
  47.         } catch (IOException e) {  
  48.             e.printStackTrace();  
  49.         }  
  50.         return code;  
  51.     }  
  52.       
  53.     /** 
  54.      * 绘制边框 
  55.      * @param g 
  56.      * @param width 
  57.      * @param height 
  58.      */  
  59.     private static void drawOutline(Graphics g, int width, int height){  
  60.         g.setColor(Color.white);  
  61.         g.fillRect(0,0,width,height);  
  62.         g.setColor(Color.BLACK);  
  63.         g.drawRect(0,0,width-1,height-1);  
  64.     }  
  65.       
  66.     /** 
  67.      * 绘制比较复杂的干扰线 
  68.      * @param g 
  69.      * @param random 
  70.      * @param width 
  71.      * @param height 
  72.      */  
  73.     private static void drawDisturb(Graphics g, Random random, int width, int height){  
  74.         int x,y,x1,y1;  
  75.         for(int i=0;i<width;i++){  
  76.             x = random.nextInt(width);  
  77.             y = random.nextInt(height);  
  78.             x1 = random.nextInt(12);  
  79.             y1 = random.nextInt(12);  
  80.             g.setColor(getRandomColor(random,120,255));  
  81.             g.drawLine(x,y,x+x1,y+y1);  
  82.             g.fillArc(x,y,x1,y1,random.nextInt(360),random.nextInt(360));  
  83.         }  
  84.     }  
  85.       
  86.     /** 
  87.      * 绘制简单的干扰线 
  88.      * @param g 
  89.      * @param random 
  90.      * @param width 
  91.      * @param height 
  92.      */  
  93.     private static void drawSimpleDisturb(Graphics g, Random random, int width, int height){  
  94.         g.setColor(getRandomColor(random,160,200));  
  95.         for (int i=0;i<155;i++)  
  96.         {  
  97.             int x = random.nextInt(width);  
  98.             int y = random.nextInt(height);  
  99.                 int xl = random.nextInt(12);  
  100.                 int yl = random.nextInt(12);  
  101.             g.drawLine(x,y,x+xl,y+yl);  
  102.         }  
  103.     }  
  104.       
  105.     /** 
  106.      * 取得随机颜色 
  107.      * @param random 
  108.      * @param pMin 
  109.      * @param pMax 
  110.      * @return 
  111.      */  
  112.     private static Color getRandomColor(Random random, int pMin,int pMax){  
  113.         pMax = (Math.abs(pMax) > 255 ? 255 : Math.abs(pMax));  
  114.         pMin = (Math.abs(pMin) > 255 ? 255 :Math.abs(pMin));  
  115.       
  116.         int r = pMin + random.nextInt(Math.abs(pMax - pMin));  
  117.         int g = pMin + random.nextInt(Math.abs(pMax - pMin));  
  118.         int b = pMin + random.nextInt(Math.abs(pMax - pMin));  
  119.       
  120.         return new Color(r,g,b);  
  121.     }   
  122.       
  123.     /** 
  124.      * 绘制验证码 
  125.      * @param g 
  126.      * @param random 
  127.      * @param validateCode 
  128.      * @param codeLength 
  129.      * @param width 
  130.      * @param height 
  131.      * @param fontSize 
  132.      * @return 
  133.      */  
  134.     private static String drawCode(Graphics g, Random random, String validateCode,int codeLength, int width, int height, int fontSize){   
  135.         int validateCodeLength = validateCode.length();  
  136.         Font font1 = new Font("Verdana",Font.BOLD,fontSize);  
  137.         Font font2 = new Font("serif",Font.BOLD,fontSize);  
  138.           
  139.         StringBuffer sb = new StringBuffer();       
  140.         int x,y;  
  141.         for(int i=0;i<codeLength;i++){  
  142.             x = (width/codeLength-1) * i + random.nextInt(width/(codeLength * 2));  
  143.             y = random.nextInt(height - fontSize) + fontSize;  
  144.             sb.append(getRandomChar(validateCode,validateCodeLength,random));  
  145.             g.setColor(getRandomColor(random,70,150));  
  146.             if(sb.substring(i).getBytes().length > 1)  
  147.             g.setFont(font2);  
  148.             else  
  149.             g.setFont(font1);  
  150.             g.drawString(sb.substring(i),x,y);  
  151.         }  
  152.         return sb.toString();       
  153.     }  
  154.       
  155.     /** 
  156.      * 取得随机字符 
  157.      * @param validateCode 
  158.      * @param validateCodeLength 
  159.      * @param random 
  160.      * @return 
  161.      */  
  162.     private static char getRandomChar(String validateCode,int validateCodeLength,Random random){  
  163.         return validateCode.charAt(random.nextInt(validateCodeLength));  
  164.     }  
  165. }  

struts.xml 配置

xml 代码
 
  1. <package name="gx80-test" namespace="/test" extends="gx80-default">  
  2.   
  3.     <action name="validateCode" class="cn.gx80.control.action.basic.ValidateImgAction">  
  4.         <interceptor-ref name="gx80DefaultStack"/>  
  5.         <interceptor-ref name="staticParams"/>  
  6.         <param name="width">100</param>  
  7.         <param name="height">30</param>  
  8.         <param name="fontSize">18</param>  
  9.         <param name="codeLength">5</param>  
  10.         <result name="success" type="stream">  
  11.             <param name="contentType">image/jpeg</param>  
  12.         </result>  
  13.     </action>  
  14.   
  15. </package>  


当然还有语言文件中的验证码字符串 global.properties

java 代码
  1. System.validateCode = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\u4E00\u4E8C\u4E09\u56DB\u4E94\u516D\u4E03\u516B\u4E5D\u5341\u767E\u5343\u4E07\u5E7F\u897F\u5357\u5B81\u79D1\u56ED\u5927\u9053\u8F6F\u4EF6\u4E2D\u56FD\u4EBA\u6C11\u4E07\u5C81  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多