1、目录ID 随机生成短ID , 使用英文字符,借助md5 的数字 DigestUtils.md5DigestAsHex(key1.getBytes());
public static String generateShortId(String key){
String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
"B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z"
};
String md5 = DigestUtils.md5DigestAsHex(key.getBytes());
String[] options = new String[4];
for (int i = 0; i < 4; i++) {
String sTempSubString = md5.substring(i * 8, i * 8 + 8);
long lHexLong = 0x3FFFFFFF & Long.parseLong(sTempSubString, 16);
String outChars = "";
for (int j = 0; j < 6; j++) {
long index = 0x0000003D & lHexLong;
outChars += chars[(int) index];
lHexLong = lHexLong >> 5;
}
options[i] = outChars;
}
return options[new Random().nextInt(options.length)];
}
2、泛型 :定义 HttpResult
public class HttpResult<T> implement Serializable {
public static <T> HttpResult<T> success(T t){
HttpResult<T> res =new HttpResult<>;
res.setContent(t);
return res;
}
}
/**
后面使用可以直接返回
调用 : HttpResult.success("测试") ;
返回类型: HttpResult ;
**/