public static void main(string[] args) { try { enumeration enumeration = networkinterface .getnetworkinterfaces(); while (enumeration.hasmoreelements()) { networkinterface networkinterface = enumeration.nextelement(); if (networkinterface.isup()) { enumeration addressenumeration = networkinterface .getinetaddresses(); while (addressenumeration.hasmoreelements()) { string ip = addressenumeration.nextelement() .gethostaddress(); final string regx_ip = ((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d); if (ip.matches(regx_ip) && !ip.equals(127.0.0.1)) { system.out.println(ip); } } } } } catch (exception e) { log.error(获取本机ip出现异常,异常信息为: + e.getmessage()); } }
