zoukankan      html  css  js  c++  java
  • Java的Guid生成

    第一种:可以用UUID类来生成GUID

    import java.util.UUID;
    public class Test {
        public static void main(String[] args) {
            for (int i=0;i<100;i++){
                UUID uuid = UUID.randomUUID();
                System.out.println (String.valueOf(uuid).length());
            }
        }
    }
    

      

    第二种:自定义工具类

    package com.gaohang.test;
     
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import java.util.Random;
     
    public class RandomGUID extends Object {
     
    	public String valueBeforeMD5 = "";
    	public String valueAfterMD5 = "";
    	private static Random myRand;
    	private static SecureRandom mySecureRand;
     
    	private static String s_id;
     
    	static {
    		mySecureRand = new SecureRandom();
    		long secureInitializer = mySecureRand.nextLong();
    		myRand = new Random(secureInitializer);
    		try {
    			s_id = InetAddress.getLocalHost().toString();
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		}
     
    	}
     
    	public RandomGUID() {
    		getRandomGUID(false);
    	}
     
    	public RandomGUID(boolean secure) {
    		getRandomGUID(secure);
    	}
     
    	private void getRandomGUID(boolean secure) {
    		MessageDigest md5 = null;
    		StringBuffer sbValueBeforeMD5 = new StringBuffer();
     
    		try {
    			md5 = MessageDigest.getInstance("MD5");
    		} catch (NoSuchAlgorithmException e) {
    			System.out.println("Error: " + e);
    		}
     
    		try {
    			long time = System.currentTimeMillis();
    			long rand = 0;
     
    			if (secure) {
    				rand = mySecureRand.nextLong();
    			} else {
    				rand = myRand.nextLong();
    			}
     
    			sbValueBeforeMD5.append(s_id);
    			sbValueBeforeMD5.append(":");
    			sbValueBeforeMD5.append(Long.toString(time));
    			sbValueBeforeMD5.append(":");
    			sbValueBeforeMD5.append(Long.toString(rand));
     
    			valueBeforeMD5 = sbValueBeforeMD5.toString();
    			md5.update(valueBeforeMD5.getBytes());
     
    			byte[] array = md5.digest();
    			StringBuffer sb = new StringBuffer();
    			for (int j = 0; j < array.length; ++j) {
    				int b = array[j] & 0xFF;
    				if (b < 0x10)
    					sb.append('0');
    				sb.append(Integer.toHexString(b));
    			}
     
    			valueAfterMD5 = sb.toString();
     
    		} catch (Exception e) {
    			System.out.println("Error:" + e);
    		}
    	}
     
    	public String toString() {
    		String raw = valueAfterMD5.toUpperCase();
    		StringBuffer sb = new StringBuffer();
    		sb.append(raw.substring(0, 8));
    		sb.append("-");
    		sb.append(raw.substring(8, 12));
    		sb.append("-");
    		sb.append(raw.substring(12, 16));
    		sb.append("-");
    		sb.append(raw.substring(16, 20));
    		sb.append("-");
    		sb.append(raw.substring(20));
     
    		return sb.toString();
    	}
    }
    

      

  • 相关阅读:
    使用postman模拟上传文件到springMVC的坑:the request was rejected because no multipart boundary was found
    win10 安装多个版本的jdk,如何切换
    String类的substring方法
    tomcat7.0配置CORS(跨域资源共享)
    win7下安装centos6.5后,开机无法进入选择双系统启动界面,只能启动centos的解决办法
    java位运算
    JDK源码--ArrayList浅析
    使用Jasperreporter生成入库出库单打印等报表操作
    centos6.5下安装zip格式的tomcat7和tomcat8,并同时运行
    Centos7配置文件共享服务器SAMBA三步曲(转)
  • 原文地址:https://www.cnblogs.com/hahayixiao/p/14927990.html
Copyright © 2011-2022 走看看