基于Java1.8
使用idea开发
功能:随机生成验证码
注:验证码位数由for循环的次数决定
package com.cn.issue;
import java.util.Random;
public class VerificationCode {
private VerificationCode(){}
//直接调用String code = VerificationCode.getVerificationCode();生成随机验证码
public static String getVerificationCode(){
String datas="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random random = new Random();
StringBuilder sb=new StringBuilder("");
for (int i = 0; i < 5; i++) {
int index = random.nextInt(62);
char ch = datas.charAt(index);
sb.append(ch);
}
return sb.toString();
}
}
调用见注释:String code = VerificationCode.getVerificationCode();