您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java获取本地IP方法

2025/8/7 2:53:13发布15次查看
获取本地服务器ip经常会出现127.0.0.1,0:0:0:0:0:0:0:1,fe80:0:0:0:960:74bd:e1a0:e5b9%11 这些情况,以下代码可解决此问题
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()); } }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product