Java_14

JDK 14 发布于 2020 年 3 月 17 日。

主要特性

Java 14 包含了下方列出的新功能,以及“数百个小改进和数千个漏洞修复” [281]

  • JEP-305:使用 instanceof 的模式匹配(预览)
  • JEP-343:打包工具(孵化中)
  • JEP-345:在 G1 中,对不均匀的内存访问(NUMA)情况下的内存申请优化
  • JEP-349:JFR Event Streaming
  • JEP-352:使字节缓冲区能够映射到非易失性存储器上
  • JEP-358:有帮助的 NullPointerExceptions
  • JEP-359:记录类(预览)
  • JEP-361:Switch 表达式(标准)
  • JEP-362:弃用 Solaris 和 SPARC 平台上的移植版本
  • JEP-363:移除 Concurrent Mark Sweep (CMS) 垃圾收集器
  • JEP-364:适用于 macOS 的 ZGC
  • JEP-365:适用于 Windows 的 ZGC
  • JEP-366:弃用 ParallelScavenge + SerialOld 的垃圾收集器组合
  • JEP-367:移除 Pack200 相关的工具及 API
  • JEP-368:文本块(第二预览版本)
  • JEP-370:外部内存访问 API(孵化中)

Terwer...大约 2 分钟后端开发JavaSEJava语言新特性版本修补漏洞预览内存jdkjavajdk14java14javase
Java生成验证码图片

生成验证码

/**
 * 验证码工具类
 *
 * @name: VerificationCode
 * @author: terwer
 * @date: 2022-07-17 22:21
 **/
public class VerificationCode {
    private static final String[] randomStr = {"0", "1", "2", "3", "4",
            "5", "6", "7", "8", "9", "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"};

    /**
     * 随机生成验证码
     *
     * @return
     */
    public static Map getVerificationCode() {
        return getVerificationCodeWithStr(null);
    }

    /**
     * 利用给定的字符串生成验证码
     *
     * @param str 指定的字符串
     * @return
     */
    public static Map getVerificationCodeWithStr(String str) {
        // 设置默认生成 4个 长度的验证码
        int strLength = 4;
        char[] strArr = null;
        if (StringUtils.isNotEmpty(str)) {
            strLength = str.length();
            strArr = str.toCharArray();
        }

        // 定义验证码图片大小
        int width = 20 * strLength + 5, height = 25;
        // 在内存中创建 图像
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 为内存中要创建的图像生成画布,
        Graphics2D graphics2D = bufferedImage.createGraphics();
        // 画一个白色矩形,作为验证码背景
        graphics2D.setColor(Color.WHITE);
        // 填充
        graphics2D.fillRect(0, 0, width, height);

        // 画 100 条 灰色的 随机干扰线
        if (StringUtils.isNotEmpty(str)) {
            graphics2D.setColor(Color.WHITE);
        } else {
            graphics2D.setColor(Color.GRAY);
        }
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            graphics2D.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height));
        }

        // 创建字体
        Font font = new Font("Times New Roman", Font.BOLD, 25);
        graphics2D.setFont(font);

        StringBuffer sb = new StringBuffer();
        // 取得 4 位数的 随机字符串
        for (int i = 0; i < strLength; i++) {
            // 返回一个 随机数,在 1 和 20 之间
            String randomNumber = randomStr[random.nextInt(36)];
            if (StringUtils.isNotEmpty(str)) {
                randomNumber = String.valueOf(strArr[i]);
            }

            int red = random.nextInt(255);
            int green = random.nextInt(255);
            int blue = random.nextInt(255);
            //获得一个随机红蓝绿的配合颜色
            graphics2D.setColor(new Color(red, green, blue));
            //把该数字用画笔在画布画出,并指定数字的坐标
            if (null != randomNumber) {
                graphics2D.drawString(randomNumber, 20 * i + 5, (height / 2) + 10);
                //把该数字加到缓存字符串中。用于等会生成验证码字符串set到session中用于校对
                sb.append(randomNumber);
            }
        }
        // 清除内存的图片
        bufferedImage.flush();
        // 释放资源
        graphics2D.dispose();

        // 返回结果
        Map result = new HashMap();
        result.put("imgStream", bufferedImage);
        result.put("code", str);
        return result;
    }
}


Terwer...大约 2 分钟经验分享验证码生成字符串随机内存javacode