nicky

积水成海,滴水穿石。

导航

<2009年10月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

统计

公告

信心十足

常用链接

留言簿(3)

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜

【转】:struts2之图片验证码实现

做注册模块,需要图片验证码机制。google了一圈,自己再整理修改了一下,总算是弄出来了。思路就是在一个action里应用java的awt包里面的类绘制一个内存中的图片,然后产生随机数并将随机数写到图片上,然后把action的返回类型设为stream,把图片数据写入到输入流返回给浏览器。html可以通过img页面直接用src属性引用该action

    action的代码如下
  1. import java.io.*;
  2. import javax.imageio.ImageIO;
  3. import javax.imageio.stream.ImageOutputStream;
  4. import java.awt.*;
  5. import java.awt.Color;
  6. import java.awt.image.BufferedImage;
  7. //DefaultAction类继承了ActionSupport 并定义了session变量
  8. public class CreateValidateAction extends DefaultAction {
  9.     private ByteArrayInputStream inputStream;
  10.     //产生四个0~9的随机数,放在一个字符串里
  11.     public String createRandomString() {
  12.         String str = "";
  13.         for (int i = 0; i < 4; i++) {
  14.             str += Integer.toString((new Double(Math.random() * 10)).intValue());
  15.         }
  16.         return str;
  17.     }
  18.     //随机产生一个颜色
  19.     public Color createsRandomColor() {
  20.         int r = (new Double(Math.random() * 256)).intValue();
  21.         int g = (new Double(Math.random() * 256)).intValue();
  22.         int b = (new Double(Math.random() * 256)).intValue();
  23.         return new Color(r, g, b);
  24.     }
  25.     //生成一个内存图片,将四个随机数写在图片上
  26.     public BufferedImage createImage(String str) {
  27.         int width = 60;
  28.         int height = 22;
  29.         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  30.         // 获取图形上下文
  31.         Graphics g = image.getGraphics();
  32.         // 设定背景色
  33.         g.setColor(Color.WHITE);
  34.         g.fillRect(00, width, height);
  35.         //画边框
  36.         g.setColor(Color.black);
  37.         g.drawRect(00, width - 1, height - 1);
  38.         // 将认证码显示到图象中
  39.         g.setFont(new Font("Atlantic Inline", Font.PLAIN, 18));
  40.         //使用随机颜色
  41.         g.setColor(this.createsRandomColor());
  42.         //将随机字符串的每个数字分别写到图片上
  43.         g.drawString(Character.toString(str.charAt(0)), 817);
  44.         g.drawString(Character.toString(str.charAt(1)), 2017);
  45.         g.drawString(Character.toString(str.charAt(2)), 3317);
  46.         g.drawString(Character.toString(str.charAt(3)), 4517);
  47.         // 图象生效
  48.         g.dispose();
  49.         return image;
  50.     }
  51.     //将图片的以字节形式写到InputStream里
  52.     public ByteArrayInputStream createInputStream() throws Exception {
  53.         //获取随机字符串
  54.         String str=this.createRandomString();
  55.         BufferedImage image = this.createImage(str);
  56.         //将产生的字符串写入session,供校验时使用
  57.         this.getSession().put("validateCode", str);
  58.         ByteArrayOutputStream output = new ByteArrayOutputStream();
  59.         ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
  60.         ImageIO.write(image, "JPEG", imageOut);
  61.         imageOut.close();
  62.         ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
  63.         output.close();
  64.         return input;
  65.     }
  66.     @Override
  67.     public String execute() throws Exception {
  68.         setInputStream(createInputStream());
  69.         return SUCCESS;
  70.     }
  71.     
  72.     public ByteArrayInputStream getInputStream() {
  73.         return inputStream;
  74.     }
  75.     public void setInputStream(ByteArrayInputStream inputStream) {
  76.         this.inputStream = inputStream;
  77.     }
  78. }

    然后是对应的struts的配置
  1.         <!--action的class是由spring提供的-->
  2.         <action name="createValidateAction" class="createValidateAction">
  3.             <result type="stream">
  4.                 <param name="contentType">image/jpeg</param>
  5.                 <param name="inputName">inputStream</param>
  6.             </result>
  7.         </action>

    最后就是html的写法,点击图片的时候可以更新验证码
  1. <script type="text/javascript">
  2.     function changeValidateCode(obj) {
  3.         //获取当前的时间作为参数,无具体意义
  4.         var timenow = new Date().getTime();
  5.         //每次请求需要一个不同的参数,否则可能会返回同样的验证码
  6.         //据说和浏览器的缓存机制有关系,不太明白,照做吧
  7.         obj.src="createValidateAction.action?d="+timenow;
  8.     }
  9. </script>
  10. <img src="createValidateAction.action" onclick="changeValidateCode(this)"/>

posted on 2009-04-23 15:47 nicky 阅读(2066) 评论(3)  编辑  收藏

评论

# re: 【转】:struts2之图片验证码实现 2009-10-28 13:18 shareach

同样配置,为什么我后台action执行了2次, 我http监视就是只有一次
我的package是
<package name="myDefPkg" extends="struts-default">
脚本是
<script type="text/javascript">
function changeValidateCode() {
var timenow = new Date().getTime();
document.getElementById("validImg").src="<%=path %>/rand.do?tick="+timenow;
}
changeValidateCode();
</script>
谢谢  回复  更多评论   

# re: 【转】:struts2之图片验证码实现 2010-03-02 14:12 xcvxcv

cxvxcvxcv  回复  更多评论   

# re: 【转】:struts2之图片验证码实现 2010-03-02 14:12 xcvxcv

dfgfgdfgdfg  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: