# java 代码

工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class RandomStringUtils {
private static final char[] codeSequence = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', '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', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'.','$'
};
/**
* 获取随机密码
* @param digits 位数
* @return
*/
public static String getCode(int digits) {
Random random = new Random();
StringBuilder randomCode = new StringBuilder();

for(int i = 0; i < digits; i++) {
randomCode.append(String.valueOf(codeSequence[random.nextInt(codeSequence.length)]));
}
return randomCode.toString();
}
}