1.验证码就是将大小写字母和数字放入到集合中,使用Collections.shuffle(list);将list集合中的数据进行打乱,遍历集合,对集合中的字符进行sb.append()操作,最后对sb进行下表截取 String result = afterShuffle.substring(5, 9);
代码
public class GenerateWord {
public static void main(String[] args) {
timer2();
}
// 第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行
// schedule(TimerTask task, long delay, long period)
public static void timer2() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
String[] beforeShuffle = new String[] { "2", "3", "4", "5", "6", "7", "8", "9", "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","g","k" };
List<String> list = Arrays.asList(beforeShuffle);
Collections.shuffle(list);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
sb.append(list.get(i));
}
String afterShuffle = sb.toString();
String result = afterShuffle.substring(5, 9);
System.out.println("验证码" + result);
}
}, 1000, 1000);
}
}