⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkimage.java

📁 简单的Java验证码实现,大家可以参考参考
💻 JAVA
字号:
package ahsl.mas2d.common.util;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.RandomStringUtils;

public class CheckImage extends HttpServlet {

            private Font mFont;
            protected String sessionAttrName;
            protected int strLength;
            protected ServletConfig filterConfig;

            public CheckImage() {
/*  24*/        strLength = 4;
/*  25*/        filterConfig = null;
/*  28*/        mFont = new Font("Times New Roman", 0, 18);
            }

            public void init(ServletConfig filterConfig) throws ServletException {
/*  31*/        this.filterConfig = filterConfig;
/*  32*/        sessionAttrName = filterConfig.getInitParameter("sessionAttrName");
/*  33*/        String length = filterConfig.getInitParameter("strLength");
/*  34*/        if (sessionAttrName == null) {
/*  35*/            sessionAttrName = "CheckImage";
                }
/*  37*/        if (length == null) {
/*  38*/            strLength = 4;
                } else {
/*  40*/            strLength = Integer.parseInt(length);
                }
            }

            public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*  46*/        doGet(request, response);
            }

            public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*  50*/        HttpSession session = request.getSession(true);
/*  51*/        response.setContentType("image/gif");
/*  52*/        response.setHeader("Pragma", "No-cache");
/*  53*/        response.setHeader("Cache-Control", "no-cache");
/*  54*/        response.setDateHeader("Expires", 0L);
/*  55*/        ServletOutputStream out = response.getOutputStream();
/*  56*/        int width = 15 * strLength;
/*  57*/        int height = 20;
/*  58*/        BufferedImage image = new BufferedImage(width, height, 1);
/*  59*/        Graphics g = image.getGraphics();
/*  60*/        g.setColor(getRandColor(200, 250));
/*  61*/        g.fillRect(0, 0, width, height);
/*  62*/        g.setFont(mFont);
/*  63*/        Random random = new Random();
/*  64*/        g.setColor(getRandColor(160, 200));
/*  65*/        for (int i = 0; i < 155; i++) {
/*  66*/            int x = random.nextInt(width);
/*  67*/            int y = random.nextInt(height);
/*  68*/            int xl = random.nextInt(12);
/*  69*/            int yl = random.nextInt(12);
/*  70*/            g.drawLine(x, y, x + xl, y + yl);
                }

/*  73*/        String sRand = RandomStringUtils.randomAlphabetic(strLength);
/*  74*/        sRand = sRand.toUpperCase();
/*  75*/        for (int i = 0; i < strLength; i++) {
/*  78*/            g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
/*  79*/            g.drawString(sRand.substring(i, i + 1), 13 * i + 6, 16);
                }

/*  82*/        session.setAttribute(sessionAttrName, sRand);
/*  83*/        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
/*  84*/        encoder.encode(image);
/*  85*/        out.close();
            }

            Color getRandColor(int fc, int bc) {
/*  89*/        Random random = new Random();
/*  90*/        if (fc > 255) {
/*  91*/            fc = 255;
                }
/*  93*/        if (bc > 255) {
/*  94*/            bc = 255;
                }
/*  96*/        int r = fc + random.nextInt(bc - fc);
/*  97*/        int g = fc + random.nextInt(bc - fc);
/*  98*/        int b = fc + random.nextInt(bc - fc);
/*  99*/        return new Color(r, g, b);
            }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -