1、uuid 依赖
<!-- uuid -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.16</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>${aliyun.oss.version}</version>
</dependency>
2、相关代码
/**
* 生成28位UUID
* @return
*/
public static String getToken(){
int first = new Random(10).nextInt(8) + 1;
System.out.println(first);
int hashCodeV = UUID.randomUUID().toString().hashCode();
if (hashCodeV < 0) {//有可能是负数
hashCodeV = -hashCodeV;
}
// 0 代表前面补充0
// 27 代表长度为27
// d 代表参数为正数型
return first + String.format("%027d", hashCodeV);
}
/**
* 生成26位可见字符的UUID
* 16位可见字符 + 10位随机数
* @return
*/
public static String getToken1(){
String uuid = IdUtil.fastSimpleUUID();
char[] cs=new char[32];
for(int i=uuid.length()/2,j=1;i-->0;){
cs[j++]=uuid.charAt(i);
}
return String.valueOf(cs) + RandomUtil.randomNumbers(10);
}
/**
* 生成UUID
* 13位随机字符 + 毫秒级的字符串
* @return
*/
public static String getToken2(){
return RandomUtil.randomString(13) + new Date().getTime();
}
}