if (os.toLowerCase ().startsWith ("win"))
{
//window伺服器IP
try
{
InetAddress address = InetAddress.getLocalHost ();
hostIP = address.getHostAddress ();
}
catch (UnknownHostException e)
{
e.printStackTrace ();
}
}
else
{
//linux伺服器IP
hostIP = LicenseUtil.getLinuxLocalIp ();
}
public static String getLinuxLocalIp ()
{
String ip = "";
try
{
for (Enumeration <NetworkInterface> en = NetworkInterface.getNetworkInterfaces (); en.hasMoreElements ();)
{
NetworkInterface intf = en.nextElement ();
String name = intf.getName ();
if (!name.contains ("docker") && !name.contains ("lo"))
{
for (Enumeration <InetAddress> enumIpAddr = intf.getInetAddresses (); enumIpAddr.hasMoreElements ();)
{
InetAddress inetAddress = enumIpAddr.nextElement ();
if (!inetAddress.isLoopbackAddress ())
{
String ipaddress = inetAddress.getHostAddress ().toString ();
if (!ipaddress.contains ("::") && !ipaddress.contains ("0:0:") && !ipaddress.contains ("fe80"))
{
ip = ipaddress;
System.out.println (ipaddress);
}
}
}
}
}
}
catch (SocketException ex)
{
System.out.println ("擷取ip位址異常");
ip = "127.0.0.1";
ex.printStackTrace ();
}
System.out.println ("IP:" + ip);
return ip;
}