2021-07-12 14:51:34 +08:00
|
|
|
|
package com.xkrs.utils;
|
|
|
|
|
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 随机字符串产生工具
|
|
|
|
|
* @author tajochen
|
|
|
|
|
*/
|
|
|
|
|
public class RandomUtil {
|
|
|
|
|
/**
|
|
|
|
|
* 获取随机字母数字组合
|
|
|
|
|
* @param length
|
|
|
|
|
* 字符串长度
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String getRandomCharAndNumr(Integer length) {
|
|
|
|
|
String str = "";
|
|
|
|
|
StringBuilder strBuffer = new StringBuilder();
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
|
boolean b = random.nextBoolean();
|
|
|
|
|
// 字符串
|
|
|
|
|
if (b) {
|
|
|
|
|
//取得65大写字母还是97小写字母
|
|
|
|
|
int choice = random.nextBoolean() ? 65 : 97;
|
|
|
|
|
strBuffer.append((char) (choice + random.nextInt(26)));
|
|
|
|
|
} else {
|
|
|
|
|
// 数字
|
|
|
|
|
strBuffer.append(random.nextInt(10));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
str = strBuffer.toString();
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 验证随机字母数字组合是否纯数字与纯字母
|
|
|
|
|
* @param str
|
|
|
|
|
* @return true 是 , false 否
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isRandomUsable(String str) {
|
|
|
|
|
// String regExp =
|
|
|
|
|
// "^[A-Za-z]+(([0-9]+[A-Za-z0-9]+)|([A-Za-z0-9]+[0-9]+))|[0-9]+(([A-Za-z]+[A-Za-z0-9]+)|([A-Za-z0-9]+[A-Za-z]+))$";
|
|
|
|
|
String regExp = "^([0-9]+)|([A-Za-z]+)$";
|
|
|
|
|
Pattern pat = Pattern.compile(regExp);
|
|
|
|
|
Matcher mat = pat.matcher(str);
|
|
|
|
|
return mat.matches();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成UUID
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String getUuid32(){
|
|
|
|
|
String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
|
|
|
|
|
return uuid;
|
|
|
|
|
// return UUID.randomUUID().toString().replace("-", "").toLowerCase();
|
|
|
|
|
}
|
2021-08-03 18:03:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 随机生成六位数
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static int returnCode() {
|
|
|
|
|
Random rand = new Random();
|
|
|
|
|
return rand.nextInt(899999) + 100000;
|
|
|
|
|
}
|
2021-07-12 14:51:34 +08:00
|
|
|
|
}
|