|
@@ -0,0 +1,116 @@
|
|
|
+package com.caimei365.user.utils;
|
|
|
+import java.util.Random;
|
|
|
+
|
|
|
+public class RandomCodeGenerator {
|
|
|
+
|
|
|
+ // TODO 与WWW的RandomCodeGenerator是重复的,后续需要删除www的RandomCodeGenerator
|
|
|
+ private static char codeSequence[] = {
|
|
|
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
|
|
|
+ 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
|
|
|
+ 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7',
|
|
|
+ '8', '9'
|
|
|
+ };
|
|
|
+
|
|
|
+ private static char intSequence[] = {
|
|
|
+ '2', '3', '4', '5', '6', '7',
|
|
|
+ '8', '9'
|
|
|
+ };
|
|
|
+
|
|
|
+ private static char stringSequence[] = {
|
|
|
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
|
|
|
+ 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
|
|
|
+ 'W', 'X', 'Y', 'Z'
|
|
|
+ };
|
|
|
+
|
|
|
+ private static char newStringSequence[] = {
|
|
|
+ 'A','a', 'B','b', 'D','d', 'E' ,'e', 'F','f', 'G','g', 'H','h',
|
|
|
+ 'L', 'N','n', 'Q','q', 'R','r', 'T','t', 'Y','y'
|
|
|
+ };
|
|
|
+// C,i,j,k,M,O,P,S,U,V,W,X,Z,l
|
|
|
+
|
|
|
+ private static char[] shortLink = {
|
|
|
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
|
|
|
+ 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'O',
|
|
|
+ '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', 'o',
|
|
|
+ 'w', 'x', 'y', 'z' ,
|
|
|
+ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
|
|
|
+ };
|
|
|
+
|
|
|
+ public static String generateShortLink(int length) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ Random random = new Random();
|
|
|
+ for (int i = 0; i < shortLink.length && i < length; ++i) {
|
|
|
+ sb.append(shortLink[random.nextInt(shortLink.length)]);
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String generateCode(int length) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ Random random = new Random();
|
|
|
+ for (int i = 0; i < codeSequence.length && i < length; ++i) {
|
|
|
+ sb.append(codeSequence[random.nextInt(codeSequence.length)]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String generateCodeInt(int length) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ Random random = new Random();
|
|
|
+ for (int i = 0; i < intSequence.length && i < length; ++i) {
|
|
|
+ sb.append(intSequence[random.nextInt(intSequence.length)]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String generateCodeString(int length) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ Random random = new Random();
|
|
|
+ for (int i = 0; i < stringSequence.length && i < length; ++i) {
|
|
|
+ sb.append(stringSequence[random.nextInt(stringSequence.length)]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 随机生成指定位数字母与数字组成的字符串
|
|
|
+ * @param length 长度
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ /*public static String generateRandomCode(int length) {
|
|
|
+ // 允许字母与数字的字符串
|
|
|
+ return RandomStringUtils.random(length, true, true);
|
|
|
+ }*/
|
|
|
+
|
|
|
+ public static String getRandomCharAndNumr(int length) {
|
|
|
+ String val = "";
|
|
|
+ Random random = new Random();
|
|
|
+ for (int i = 0; i < length; i++) {
|
|
|
+ // 输出字母还是数字
|
|
|
+ String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
|
|
|
+ // 字符串
|
|
|
+ if ("char".equalsIgnoreCase(charOrNum)) {
|
|
|
+ // 取得大写字母还是小写字母
|
|
|
+ int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
|
|
|
+ val += (char) (choice + random.nextInt(26));
|
|
|
+ } else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
|
|
|
+ val += String.valueOf(random.nextInt(10));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return val;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String generateAccount(int length){
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ Random random = new Random();
|
|
|
+ for (int i = 0; i < newStringSequence.length && i < length; ++i) {
|
|
|
+ sb.append(newStringSequence[random.nextInt(newStringSequence.length)]);
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+}
|