Commit 49eb783d by 林冲

修改车源号生成

1 parent 8d941f6d
package com.gsc4cloud.gsc.vpc.api.util;
import org.springframework.stereotype.Component;
import java.security.SecureRandom;
/**
* @BelongsProject: glsx-accounts-test
......@@ -10,42 +10,27 @@ import org.springframework.stereotype.Component;
* @Description: TODO
* @Version: 1.0
*/
@Component
public class ShortLinkComponent {
private static final SecureRandom RANDOM = new SecureRandom();
/**
* 62个字符
*/
private static final String CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* 生成短链码
* @param param
* @return
*/
public String createShortLinkCode(String param){
long murmurhash = CommonUtil.murmurHash32(param);
//进制转换
String code = encodeToBase62(murmurhash);
return code;
}
/**
* 10进制转62进制
* @param num
* @return
* 随机生成指定长度的字符串
* @param length 长度
* @return 随机字符串
*/
private String encodeToBase62(long num){
// StringBuffer线程安全,StringBuilder线程不安全
StringBuffer sb = new StringBuffer();
do{
int i = (int )(num%62);
sb.append(CHARS.charAt(i));
num = num/62;
}while (num>0);
String value = sb.reverse().toString();
return value;
public static String randomCode(int length) {
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = RANDOM.nextInt(CHARS.length());
sb.append(CHARS.charAt(index));
}
return sb.toString();
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!