import java.util.concurrent.ThreadLocalRandom;
/*
生成验证码的工具
可动态配置验证码长度
*/
public class CodeUtils {
public static void main(String[] args) {
//随机生成5个长度为4的验证码
for (int i = 0; i < 5; i++) {
System.out.println(CodeUtils.getCode(4));
}
for (int i = 0; i < 5; i++) {
System.out.println(CodeUtils.getCode(6));
}
}
public static String getCode(int len){
//验证码生成范围
String s = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
char[] c = s.toCharArray();
StringBuffer str = new StringBuffer("");
ThreadLocalRandom current = ThreadLocalRandom.current();
for (int i = 0; i < len; i++) {
char code = c[current.nextInt(0,s.length())];
str.append(code);
}
return str.toString();
}
}
运行结果:
文章来源地址https://www.toymoban.com/news/detail-634719.html
文章来源:https://www.toymoban.com/news/detail-634719.html
到了这里,关于java实现随机生成验证码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!