/**
* 生成指定位数的验证码,包含数字及大小写字母
* @param len 验证码位数
* @return
*/
public static String getVerifyCode(int len) {
Random random = new SecureRandom();
String s = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] ch = new char[len];
for(int i = 0; i< len; i++){
int index = random.nextInt(s.length()); // 下标
char c = s.charAt(index); // 返回指定索引处的char的值
ch[i] = c;
}
String str = new String(ch);
return str;
}
public static void main(String[] args) {
System.out.println(getVerifyCode(4)); // yogB
}
参考文章链接: