运用Struts2.0实现页面中的验证码


<1>画验证码核心类ValidateCodeAction

  1. package com.tarena.common.action;  
  2.   
  3. import java.awt.*;  
  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. import com.opensymphony.xwork2.ActionContext;  
  11. /** 
  12.  * 网页中验证码的实现 
  13.  * @author JimmyZhang 
  14.  * @since 2012.5.10 
  15.  * 
  16.  */  
  17. public class ValidateCodeAction {  
  18.       
  19.     private ByteArrayInputStream inputStream;  
  20.     //默认的执行方法   
  21.     public String execute() throws Exception{  
  22.         Random r = new Random();  
  23.         //BufferedImage相当与缓存在内存中的图像   
  24.         BufferedImage image =   
  25.         new BufferedImage(60,//绘图区域的长度   
  26.                           20,//绘图区域的高度   
  27.                           BufferedImage.TYPE_INT_RGB);  
  28.         //获取绘图工具Graphiscs   
  29.         Graphics g = image.getGraphics();  
  30.         //设置绘图颜色   
  31.         g.setColor(new Color(r.nextInt(255),  
  32.                              r.nextInt(255),  
  33.                              r.nextInt(255)));  
  34.         //从原点(0,0)填充绘图区域   
  35.         g.fillRect(006020);  
  36.         //生成一个随机的字符串(5位数字)   
  37.         String number = String.valueOf(r.nextInt(99999));  
  38.         //将number绘制在image中   
  39.         g.setColor(new Color(0,0,0));  
  40.         g.drawString(number, 515);  
  41.         //将number保存在Session中   
  42.         ActionContext ac = ActionContext.getContext();  
  43.         ac.getSession().put("vcode", number);  
  44.         //画干扰线   
  45.         for(int i=0; i<3;i++){  
  46.             drawLine(g,r);  
  47.         }  
  48.         //写入到字节输出流中   
  49.          ByteArrayOutputStream output = new ByteArrayOutputStream();  
  50.          ImageOutputStream imageOutput =   
  51.                  ImageIO.createImageOutputStream(output);  
  52.          //将图像image写入到imageOutput中   
  53.          ImageIO.write(image, "jpeg", imageOutput);  
  54.          //根据output构建inputStream   
  55.          inputStream = new ByteArrayInputStream(  
  56.                  output.toByteArray());  
  57.          //对象之间的转换--image-->ByteArrayOutStream-->ByteArrayInputStream   
  58.          return "success";   
  59.     }  
  60.       
  61.     //辅助方法,用于绘制一条干扰线   
  62.     private void drawLine(Graphics g,Random r){  
  63.         g.setColor(new Color(r.nextInt(255),  
  64.                              r.nextInt(255),  
  65.                              r.nextInt(255)));  
  66.         //drawLine(x1,y1,x2,y2)   
  67.         g.drawLine(  
  68.                  r.nextInt(60), r.nextInt(20),  
  69.                  r.nextInt(60), r.nextInt(20));  
  70.     }  
  71.       
  72.     public ByteArrayInputStream getInputStream() {  
  73.         return inputStream;  
  74.     }  
  75.   
  76.     public void setInputStream(ByteArrayInputStream inputStream) {  
  77.         this.inputStream = inputStream;  
  78.     }  
  79.       
  80. }  
  • 1
  • 2
  • 3
  • 下一页

相关内容