一个生成验证码的类

package com.huawei.tsm.business.login.common;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huawei.bme.commons.om.log.DebugLog;
import com.huawei.bme.commons.om.log.LogFactory;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
*
* 验证码生成类
* @author zxp
* @version MTV Portal V100R001C01 2009-2-5
* @since MTV Portal V100R001C01
*/
public class RandomPictrueServlet extends HttpServlet
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /**
     * 日志实例
     */
    private final static DebugLog debugLog = LogFactory.getDebugLog(RandomPictrueServlet.class);

    private static final String CONTENT_TYPE = "image/jpeg";

    private static final int WIDTH = 70;

    private static final int HEIGHT = 18;

    private static final int LENGTH = 4;

    public static final String SESSION_AUTHEN_CODE = "session_picCode";

    // Initialize global variables
    public void init() throws ServletException
    {
    }

    // Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {

        response.setContentType(CONTENT_TYPE);
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        int width = WIDTH;
        int height = HEIGHT;
        int length = LENGTH;
        String sessionName = request.getParameter("authenName");
        if ((null == sessionName) || ("".equals(sessionName.trim())))
        {
            sessionName = SESSION_AUTHEN_CODE;
        }
        try
        {
            width = Integer.parseInt(request.getParameter("width"));
        }
        catch (Exception ex)
        {
            width = WIDTH;
        }
        try
        {
            height = Integer.parseInt(request.getParameter("heigth"));
        }
        catch (Exception ex)
        {
            height = HEIGHT;
        }
        try
        {
            length = Integer.parseInt(request.getParameter("length"));
        }
        catch (Exception ex)
        {
            length = LENGTH;
        }
        try
        {

            Random ran = new Random(System.currentTimeMillis());

            String seed = (ran.nextInt() + "").substring(1, 1 + length);

            request.getSession().setAttribute(sessionName, seed);

            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < seed.length(); i++)
            {
                sb.append(seed.charAt(i) + "  ");
            }
            String sb_str = sb.toString();

            synchronized (this)
            {
                BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

                Graphics2D g = (Graphics2D) image.getGraphics();

                g.setColor(new Color(198, 221, 254));

                g.fillRect(0, 0, width, height);
                g.setColor(new Color(165, 199, 255));
                g.drawRect(0, 0, width - 1, height - 1);
                g.setColor(new Color(255, 255, 255));
                g.drawRect(1, 1, width - 3, height - 3);

                g.setColor(Color.DARK_GRAY);

                for (int i = 0; i < 4; i++)
                {
                    int x_b = ran.nextInt(width);
                    int x_e = ran.nextInt(width);
                    int y_b = ran.nextInt(height + 10);
                    int y_e = ran.nextInt(height + 10);
                    g.drawLine(x_b, y_b, x_e, y_e);
                }

                g.setFont(new Font("System", Font.BOLD, 14));

                g.drawString(sb_str, 5, 15);

                g.dispose();
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
                encoder.encode(image);
            }
        }
        catch (Exception ex)
        {
            debugLog.error(ex.toString(), ex);
        }
    }

    // Clean up resources
    public void destroy()
    {
    }
}

你可能感兴趣的:(验证码)