起因是公司一个springboot项目启动类打印了本机ip地址加端口号,方便访问项目页面,但是发现打印出来的不是“无线局域网”的ip而是“以太网适配器”ip,如下图所示

这样就导致后续本地起项目连接xxl-job注册节点的时候因为不在同个局域网下ping不通出问题,所以需要解决一下。
一、直接获取本机ip(会受到虚拟机干扰)
public static string getinterip1() throws exception {
return inetaddress.getlocalhost().gethostaddress();
}二、获取本机ip(排除虚拟机干扰)
public static inetaddress getlocalhostlanaddress() throws unknownhostexception {
try {
inetaddress candidateaddress = null;
// 遍历所有的网络接口
for (enumeration ifaces = networkinterface.getnetworkinterfaces(); ifaces.hasmoreelements();) {
networkinterface iface = (networkinterface) ifaces.nextelement();
// 在所有的接口下再遍历ip
for (enumeration inetaddrs = iface.getinetaddresses(); inetaddrs.hasmoreelements();) {
inetaddress inetaddr = (inetaddress) inetaddrs.nextelement();
if (!inetaddr.isloopbackaddress()) {// 排除loopback类型地址
if (inetaddr.issitelocaladdress()) {
// 如果是site-local地址,就是它了
return inetaddr;
} else if (candidateaddress == null) {
// site-local类型的地址未被发现,先记录候选地址
candidateaddress = inetaddr;
}
}
}
}
if (candidateaddress != null) {
return candidateaddress;
}
// 如果没有发现 non-loopback地址.只能用最次选的方案
inetaddress jdksuppliedaddress = inetaddress.getlocalhost();
if (jdksuppliedaddress == null) {
throw new unknownhostexception("the jdk inetaddress.getlocalhost() method unexpectedly returned null.");
}
return jdksuppliedaddress;
} catch (exception e) {
unknownhostexception unknownhostexception = new unknownhostexception(
"failed to determine lan address: " + e);
unknownhostexception.initcause(e);
throw unknownhostexception;
}
}三、获取本机公网ip
public static string getoutip() {
try {
url whatismyip = new url("http://checkip.amazonaws.com");
bufferedreader in = new bufferedreader(new inputstreamreader(whatismyip.openstream()));
string ip = in.readline();
return ip;
} catch (exception e) {}
return "";
}四、测试
public class iptest {
public static void main(string[] args) throws exception {
system.out.println("获取本机ip: " + getinterip1());
// system.out.println("getinterip2: " + getinterip2());
system.out.println("获取本机ip(排除虚拟机干扰): " + string.valueof(getlocalhostlanaddress()).substring(1));
// system.out.println("getoutipv4: " + getoutipv4());
system.out.println("获取本机公网ip: " + getoutip());
}
public static string getinterip1() throws exception {
return inetaddress.getlocalhost().gethostaddress();
}
public static inetaddress getlocalhostlanaddress() throws unknownhostexception {
try {
inetaddress candidateaddress = null;
// 遍历所有的网络接口
for (enumeration ifaces = networkinterface.getnetworkinterfaces(); ifaces.hasmoreelements();) {
networkinterface iface = (networkinterface) ifaces.nextelement();
// 在所有的接口下再遍历ip
for (enumeration inetaddrs = iface.getinetaddresses(); inetaddrs.hasmoreelements();) {
inetaddress inetaddr = (inetaddress) inetaddrs.nextelement();
if (!inetaddr.isloopbackaddress()) {// 排除loopback类型地址
if (inetaddr.issitelocaladdress()) {
// 如果是site-local地址,就是它了
return inetaddr;
} else if (candidateaddress == null) {
// site-local类型的地址未被发现,先记录候选地址
candidateaddress = inetaddr;
}
}
}
}
if (candidateaddress != null) {
return candidateaddress;
}
// 如果没有发现 non-loopback地址.只能用最次选的方案
inetaddress jdksuppliedaddress = inetaddress.getlocalhost();
if (jdksuppliedaddress == null) {
throw new unknownhostexception("the jdk inetaddress.getlocalhost() method unexpectedly returned null.");
}
return jdksuppliedaddress;
} catch (exception e) {
unknownhostexception unknownhostexception = new unknownhostexception(
"failed to determine lan address: " + e);
unknownhostexception.initcause(e);
throw unknownhostexception;
}
}
public static string getoutip() {
try {
url whatismyip = new url("http://checkip.amazonaws.com");
bufferedreader in = new bufferedreader(new inputstreamreader(whatismyip.openstream()));
string ip = in.readline();
return ip;
// system.out.println("public ip address: " + ip);
} catch (exception e) {
// system.out.println("error occurred: " + e.getmessage());
}
return "";
}
}获取本机ip: 172.17.16.1 获取本机ip(排除虚拟机干扰): 192.168.100.100 获取本机公网ip: 14.145.43.147
这里打印出来的便对应上前言命令行截图里的ip信息,然后公网ip对应的是百度搜索ip查到的公网ip地址
五、手动禁用虚拟机排除干扰
除了用代码逻辑排除虚拟机干扰,我们也可以直接禁用电脑的虚拟机适配器。
步骤:
- win+r
- devmgmt.msc 打开设备管理器
- 找到网络适配器-hyper-v virtual 开头的 右键禁用

此时win+r cmd 输入ipconfig,可以看到已经没有虚拟机ip了

此时java使用
inetaddress.getlocalhost().gethostaddress()
也可以获取到192.168开头的wifi地址了
总结
到此这篇关于java获取本机ip地址的文章就介绍到这了,更多相关java获取本机ip地址内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论