随机验证码
// 生成随机验证码
public static String createCode(int n) {
String code = "";
Random r = new Random();
for (int i = 0; i < n; i++) {
int type = r.nextInt(3);
switch (type) {
case 0:
// 大写字符 (A 65 - Z 65+25)
char ch = (char) (r.nextInt(26) + 65);
code += ch;
break;
case 1:
// 小写字符 (a 97 - z 97+25)
char ch1 = (char) (r.nextInt(26) + 97);
code += ch1;
break;
case 2:
// 数字
code += r.nextInt(10);
break;
}
}
return code;
}
}
网友评论