java获取本地IP地址集合包括虚拟机的ip

public static ArrayList<String> getLocalIpAddr()
{
ArrayList<String> ipList = new ArrayList<String>();
InetAddress[] addrList;
try
{
Enumeration interfaces=NetworkInterface.getNetworkInterfaces();
while(interfaces.hasMoreElements())
{
NetworkInterface ni=(NetworkInterface)interfaces.nextElement();
Enumeration ipAddrEnum = ni.getInetAddresses();
while(ipAddrEnum.hasMoreElements())
{
InetAddress addr = (InetAddress)ipAddrEnum.nextElement();
if (addr.isLoopbackAddress() == true)
{
continue;
} String ip = addr.getHostAddress();
if (ip.indexOf(":") != -1)
{
//skip the IPv6 addr
continue;
}
ipList.add(ip);
}
} Collections.sort(ipList);
}
catch (Exception e)
{
e.printStackTrace();
} return ipList;
}
上一篇:Python项目使用memcached缓存


下一篇:Java获取本地IP地址和主机名