您的位置:首页 > 编程语言 > Java开发

Java生成随机验证码

2017-11-14 10:04 253 查看
代码如下:

import java.util.Random;

public class VerifyCodeUtils {
public static String getGenerateVerifyCode() {
int maxNum = 36;
char[] chars = { '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' };

int count = 0;
int i;
String str = "";
Random r = new Random();
while (count < 6) { // default 6 length
i = Math.abs(r.nextInt(maxNum));

if (i >= 0 && i < chars.length) {
str += chars[i];
count++;
}
}

return str.toUpperCase();
}

public static void main(String[] args) {
System.out.println(getGenerateVerifyCode());
}
}


注意:请使用者自行校验是否重复,是否同时包含大小写字母、数字
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: