package cn.ksource.core.util; import java.security.MessageDigest; import java.util.UUID; /** * Created by chenlong * Date:2017/7/30 * time:11:10 */ public class GenerateUtil { public static String generateUUID() { return UUID.randomUUID().toString().replaceAll("-", ""); } public static String generate16Id() { String uuid = generateUUID(); String nextId = null; char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(uuid.getBytes()); byte tmp[] = md.digest(); char str[] = new char[16]; int k = 0; for (int i = 0; i < 16; i++) { byte byte0 = tmp[i]; //只取高位 str[k++] = hexDigits[(byte0 >>> 4 & 0xf) % 10]; // str[k++] = hexDigits[byte0 & 0xf]; } nextId = new String(str); // 换后的结果转换为字符串 } catch (Exception e) { e.printStackTrace(); } return nextId; } public static void main(String[] args) { for (int i = 0; i < 200; i++) { System.out.println(generate16Id()); } } }