加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java生成汉字验证码

发布时间:2020-12-14 23:53:01 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.util.*;public class CheckCode { public static void main(String[] args) { String code = ""; Random random = new Random(); String[

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

import java.util.*;

public class CheckCode {
    public static void main(String[] args) {

        String code = "";

        Random random = new Random();

        String[] rBase = { "0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f" };
        // 生成第1位的区码
        int r1 = random.nextInt(3) + 11; // 生成11到14之间的随机数
        String str_r1 = rBase[r1];
        // 生成第2位的区码
        int r2;
        if (r1 == 13) {
            r2 = random.nextInt(7); // 生成0到7之间的随机数
        } else {
            r2 = random.nextInt(16); // 生成0到16之间的随机数
        }
        String str_r2 = rBase[r2];
        // 生成第1位的位码
        int r3 = random.nextInt(6) + 10; // 生成10到16之间的随机数
        String str_r3 = rBase[r3];
        // 生成第2位的位码
        int r4;
        if (r3 == 10) {
            r4 = random.nextInt(15) + 1; // 生成1到16之间的随机数
        } else if (r3 == 15) {
            r4 = random.nextInt(15); // 生成0到15之间的随机数
        } else {
            r4 = random.nextInt(16); // 生成0到16之间的随机数
        }
        String str_r4 = rBase[r4];
        System.out.println(str_r1 + str_r2 + str_r3 + str_r4);

        // 将生成机内码转换为汉字
        byte[] bytes = new byte[2];
        // 将生成的区码保存到字节数组的第1个元素中
        String str_r12 = str_r1 + str_r2;
        int tempLow = Integer.parseInt(str_r12,16);
        bytes[0] = (byte) tempLow;
        // 将生成的位码保存到字节数组的第2个元素中
        String str_r34 = str_r3 + str_r4;
        int tempHigh = Integer.parseInt(str_r34,16);
        bytes[1] = (byte) tempHigh;

        code = new String(bytes); // 根据字节数组生成汉字
        System.out.println("生成汉字:" + code);
    }
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读