你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2012-07-30] 来源:Linux社区  作者:jimmy5z [字体:大 中 小]

<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(0, 0, 60, 20);  
  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, 5, 15);  
  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. }  
linux
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/4/16 20:01:09
zhima365.com
很实用