package com.qiwu.inference.common.utils; import com.qiwu.inference.common.cache.ExpireThread; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @Component public class MD5Util { //玩家数据路径 public static String userFilePath; @Value("${userFilePath}") public void setUserFilePath(String userFilePath) { MD5Util.userFilePath = userFilePath; } //玩家系列汇总数据路径 public static String dataFilePath; @Value("${dataFilePath}") public void setDataFilePath(String dataFilePath) { MD5Util.dataFilePath = dataFilePath; } /** * 使用md5进行加密 */ public static String md5(String text) { byte[] secretBytes = null; try { secretBytes = MessageDigest.getInstance("md5").digest(text.getBytes()); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("没有md5的算法"); } StringBuilder sb = new StringBuilder(40); for(byte x:secretBytes) { if((x & 0xff)>>4 == 0) { sb.append("0").append(Integer.toHexString(x & 0xff)); } else { sb.append(Integer.toHexString(x & 0xff)); } } return sb.toString(); } /** * 将uid转成对应的路径 */ public static String changeToPath(String uid, String seriesName) { //创建玩家数据路径 String md5 = md5(uid); String front = md5.substring(0,2); String second = md5.substring(2, 4); String three = md5.substring(4, 6); return userFilePath + "/" + front + "/" + second+ "/" + three+ "/" + uid + "/" + seriesName + "/"; } /** * 将uid转成系列汇总数据的路径 */ public static String changeToDataPath(String uid, String seriesName) { //创建玩家数据路径 String md5 = md5(uid); String front = md5.substring(0,2); String second = md5.substring(2, 4); String three = md5.substring(4, 6); return dataFilePath+ "/" + seriesName + "/" + front + "/" + second+ "/" + three+ "/"; } public static void main(String[] args) { String uid = "Test12321211123"; System.out.println("路径是:" + changeToPath(uid, "测试")); } }