• 验证码


    代码

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 让浏览器3秒自动刷新一次;
        response.setHeader("refresh", "10");
    
        // 在内存中创建一个图片
        BufferedImage image = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB);
        // 得到笔
        Graphics2D g = (Graphics2D) image.getGraphics();
        // 设置笔的颜色
        g.setColor(Color.decode("#FFFFFF"));
        // 画满图片
        g.fillRect(0, 0, 80, 20);
        // 设置笔的颜色
        g.setColor(Color.BLUE);
        // 设置笔的字体
        g.setFont(new Font(null, 1, 18));
        // 写字
        String randomNum = getRandomNum(g);
        System.out.println(randomNum);
    
        // 随机产生15条干扰线,使图象中的认证码不易被其它程序探测到。
        g.setColor(Color.decode("#000000"));
        Random random = new Random();
        for (int i = 0; i < 15; i++) {
            int x = random.nextInt(80);
            int y = random.nextInt(20);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x, y, x + xl, y + yl);
        }
    
        // 告诉浏览器,这个请求用图片的方式打开
        response.setContentType("image/jpeg");
        // 网站存在缓存,不让浏览器缓存
        response.setDateHeader("expires", -1);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
    
        // 把图片写给浏览器
        ImageIO.write(image, "jpg", response.getOutputStream());
    }
    
    // 画上随机数
    private String getRandomNum(Graphics2D g) {
        char[] codeSequence = { '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','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' };
    
        StringBuilder randomCode = new StringBuilder();
        int red = 0, green = 0, blue = 0;
        Random random = new Random();
    
        // 随机产生codeCount数字的验证码。
        for (int i = 0; i < 5; i++) {
            // 得到随机产生的验证码数字。
            String code = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
            // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
            red = random.nextInt(255);
            green = random.nextInt(255);
            blue = random.nextInt(255);
    
            // 用随机产生的颜色将验证码绘制到图像中。
            g.setColor(new Color(red, green, blue));
            g.drawString(code, (i + 1) * 12, 19);
    
            // 将产生的四个随机数组合在一起。
            randomCode.append(code);
        }
    
        return randomCode.toString();
    }
    

    测试

  • 相关阅读:
    Python3之random模块常用方法
    Go语言学习笔记(九)之数组
    Go语言学习笔记之简单的几个排序
    Go语言学习笔记(八)
    Python3之logging模块
    Go语言学习笔记(六)
    123. Best Time to Buy and Sell Stock III(js)
    122. Best Time to Buy and Sell Stock II(js)
    121. Best Time to Buy and Sell Stock(js)
    120. Triangle(js)
  • 原文地址:https://www.cnblogs.com/shenleg/p/14253418.html
Copyright © 2020-2023  润新知