struts2之动态生成简单验证码

Action:

public class ValidateCodeAction {
    //产生验证码图片的基本常量数据
    private static final int WIDTH=200;
    private static final int HEIGHT=80;
    //验证码的构成(26个大写字母和0-9的数字)
    //共36个字符,在浏览器中显示的验证码应由5位构成
    private static final int NUM=5;
    private static char[] seq=
        {'A','B','C','D','E','F','G',
        'H','I','J','K','L','M','N','O','P','Q',
        'R','S','T','U','V','W','X','Y','Z',
        '0','1','2','3','4','5','6','7','8','9'};
    /**
     * 要求action为结果对象(StreamResult)准备输入流
     * 在配置文件中<restult>标签所配置的就跟对象要求
     * action为其提供一个输入流,结果可以将这个输入流
     * 在浏览器中输出
     */
    private InputStream input;
    public InputStream getInput() {
        return input;
    }
    public void setInput(InputStream input) {
        this.input = input;
    }
    /**
     *通过Random对象,产生一个随机颜色对象
     *new Color(R,G,B)
     */
    private Color randomColor(Random r){
        return new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
    }
    /**
     * action 中默认的请求执行方法
     */
    public String execute(){
        byte[] imagebyte=randomImage();
        input=new ByteArrayInputStream(imagebyte);
        return "success";
    }
    
    /**
     * 获取验证码图像的字节数组
     * ①创建内存图像-BufferedImage;
     * ②通过图形绘制验证码;
     * ③将验证码图像转变为字节数组
     */
    private byte[] randomImage(){
        Random r=new Random();
        //内存图像
        BufferedImage image=new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
        //通过内存图像获得绘图对象
        Graphics g=image.getGraphics();
        //①设置颜色
        g.setColor(randomColor(r));
        //画一个实心的矩形
        g.fillRect(0, 0, WIDTH, HEIGHT);
        
        //StringBuffer sb用于存储产生的随机字符
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<NUM;i++){
            g.setColor(randomColor(r));
            int h=(int)(HEIGHT*60/100*r.nextDouble()+(HEIGHT*30/100));
            //设置字体风格样式及大小
            g.setFont(new Font(null,Font.BOLD|Font.ITALIC,h));
            String ch=String.valueOf(seq[r.nextInt(seq.length)]);
            sb.append(ch);
            //画出具体字符
            int h1=h+r.nextInt(HEIGHT-h+1);
            g.drawString(ch, i*WIDTH*90/NUM/100+r.nextInt(15)-7,h1);
            
        }
        //绘制干扰线
        for(int i=0;i<=12;i++){
            g.setColor(randomColor(r));
            g.drawLine(r.nextInt(WIDTH), r.nextInt(HEIGHT),r.nextInt(WIDTH), r.nextInt(HEIGHT));
        }
        /**
         * 创建输出流
         * FileOutputStream
         */
        ByteArrayOutputStream os=new ByteArrayOutputStream();
        try {
            //利用ImageIO类,将缓存图像写入到输出流中
            ImageIO.write(image, "JPEG", os);
            return os.toByteArray();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}

struts.xml:

<action name="imageAction" class="action.ValidateCodeAction">
            <result name="success" type="stream">
                <param name="inputName">input</param>
                <param name="contentType">image/jpeg</param>
            </result>
</action>

Html:

  <img src="imageAction">

posted @ 2018-07-20 18:07  nightbird  阅读(83)  评论(0)    收藏  举报