package com.task.util;java
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; import java.util.Random;服務器
/**app
-
@Author: wangzwdom
-
@Date: 2017/11/10 13:59oop
-
@Description:ui
-
@Version: 1.0 */ public class IpUtils {.net
public static String getIpUuid(){ StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(getServerIp()).append("-").append(getRandomString(5)); return stringBuffer.toString(); }ip
private static String getRandomString(int length){ String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random random = new Random(); StringBuffer stringBuffer = new StringBuffer(); for(int i = 0 ; i < length; ++i){ stringBuffer.append(str.charAt(random.nextInt(62))); } return stringBuffer.toString().toUpperCase(); }get
/**string
- @Author: wangzw
- @Description: 獲得服務器IP
- @Version: 1.0
- @Date: 2017/9/2 15:45 */ public static String getServerIp() { String SERVER_IP = null; try { Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; boolean finded = false;// 是否找到外網IP while (netInterfaces.hasMoreElements() && !finded) { NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); Enumeration address = ni.getInetAddresses(); while (address.hasMoreElements()) { ip = (InetAddress) address.nextElement(); if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) { SERVER_IP = ip.getHostAddress(); finded = true; break; } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) { SERVER_IP = ip.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } return SERVER_IP; }
}