1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.Test;
import java.net.InetAddress; import java.net.NetworkInterface; import java.util.UUID;
public class UuidUtil {
private static Logger log = LogManager.getLogger(UuidUtil.class); private static String HASH_MAC = "";
public static 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" };
public static String generateShortUuid() { StringBuffer shortBuffer = new StringBuffer(); String uuid = UUID.randomUUID().toString().replace("-", ""); for (int i = 0; i < 8; i++) { String str = uuid.substring(i * 4, i * 4 + 4); int x = Integer.parseInt(str, 16); shortBuffer.append(chars[x % 0x3E]); } return shortBuffer.toString(); }
public static String getUuidMixMac(){
if("".equals(HASH_MAC)) { try { HASH_MAC = getMACAddress().hashCode() + ""; } catch (Exception e) { log.error(e.getMessage()); } } String s = UUID.randomUUID().toString(); return (HASH_MAC+UUID.randomUUID().toString()).replace("-", "");
}
public static String getMACAddress() throws Exception { byte[] mac = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < mac.length; i++) { if (i != 0) { sb.append("-"); } String s = Integer.toHexString(mac[i] & 0xFF); sb.append(s.length() == 1 ? 0 + s : s); } return sb.toString().toUpperCase(); }
public static void main(String [] arg) throws Exception{
System.out.println(getMACAddress()); System.out.println(getUuidMixMac()); System.out.println(generateShortUuid()); } }
|