一个大小写加数字的字符串生成工具,可以指定长度,没什么技术含量,直接贴代码了:
/**
*
* Description:
* 生成验证码
* @author: mushi
* @Date: 2021/2/6 9:31
*/
public class CheckCode {
/**验证码*/
private String check = "";
List<Integer> reUse = new ArrayList<>();
char[] charCode = {'1','2','3','4','5','6','7','8','9','0','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',
'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'};
/**num:验证码位数*/
public String getCheckCode(int num){
char[] result = new char[num];
for (int k = 0;k<num;k++){
int index = new Random().nextInt(charCode.length);
char code = charCode[index];
int randow = getRandow(num);
result[randow] = code;
}
for (int i = 0;i<result.length;i++){
check += String.valueOf(result[i]);
}
return check;
}
/**排位*/
public int getRandow(int round){
int checkNum = new Random().nextInt(round);
if (!reUse.contains(checkNum)){
reUse.add(checkNum);
}else{
return this.getRandow(round);
}
return checkNum;
}
}
测试类:
public class Test {
public static void main(String[] args) {
CheckCode checkCode = new CheckCode();
//getCheckCode中指定长度
String code = checkCode.getCheckCode(6);
System.out.println(code);
}
}