java下linux和window通用的获取指定网段的本地ip地址(NetworkInterface)

我在工作中需要跨系统的需求,找到了NetworkInterface方法,匹配出192开头的本地网卡地址.


String ipaddress = "";

InetAddress ip = null;
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {


NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();


        // 遍历所有ip
        Enumeration<InetAddress> ips = ni.getInetAddresses();
        while (ips.hasMoreElements()) {


//我的需求是匹配192开头的地址
        ip = (InetAddress) ips.nextElement();
String fip = ip.toSting();
String sip = fip.subString(1,5);
String cmstr = '192.';
if (sip.equals(cmstr)){


ipaddress = sip.subString(1,fip.length());
}
}


}

你可能感兴趣的:(java下linux和window通用的获取指定网段的本地ip地址(NetworkInterface))