java生成验证码

来源:互联网 发布:apache spark 版本 编辑:程序博客网 时间:2024/05/30 02:25
public static void main(String args[]) {
        String str = "";
        for (int i = 0; i <= 9; i++) {
            if (StringUtils.isBlank(str)) {
                str += i;
            } else {
                str = str + "," + i;
            }
        }
        for (int i = 97; i <= 122; i++) {
            str = str + "," + (char) i;
        }
        for (int i = 65; i <= 90; i++) {
            str = str + "," + (char) i;
        }
        str="按,时,及,问,哦,如,果,你,搜,的,佛,个,安,慰,爱,了,就,发,生,的,就,爱,上,了,啊,额,为";
        System.out.println(str);
        String str2[] = str.split(",");//将字符串以,分割


        Random rand = new Random();//创建Random类的对象rand  
        int index = 0;
        String randStr = "";//创建内容为空字符串对象randStr  
        for (int i = 0; i < 4; ++i) { //循环,生成对应长度的验证码
            index = rand.nextInt(str2.length - 1);//在0到str2.length-1生成一个伪随机数赋值给index  
            System.out.println(index);
            randStr += str2[index]; //取出对应下标的值  
        }
        System.out.println("验证码:" + randStr);//输出验证码
        
        //java调用cmd
        try {
            Runtime.getRuntime().exec("notepad");
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
0 0
原创粉丝点击