java獲取本機ip(排除虛擬機等一些ip)最終解,總算找到方法了


本文參考https://blog.csdn.net/u011809209/article/details/77236602

本文參考https://blog.csdn.net/yinshuomail/article/details/81624648

首先,你如果搜索“JAVA獲取本機IP地址”,基本上搜到的資料全是無用的。 
實際上的代碼在復雜環境下是不准的

網上一個比較普遍的說法是InetAddress.getLocalHost().getHostAddress() 
似乎很簡單,但忽略了一個問題,即IP地址在現在的網絡環境更加復雜了,比如有Lan,WIFI,藍牙熱點,虛擬機網卡… 
即存在很多的網絡接口(network interfaces),每個網絡接口就包含一個IP地址,並不是所有的IP地址能被外部或局域網訪問,比如說虛擬機網卡地址等等。 
也就是說InetAddress.getLocalHost().getHostAddress()的IP不一定是正確的IP。

寫代碼前,先明確一些規則:

127.xxx.xxx.xxx 屬於”loopback” 地址,即只能你自己的本機可見,就是本機地址,比較常見的有127.0.0.1; 
192.168.xxx.xxx 屬於private 私有地址(site local address),屬於本地組織內部訪問,只能在本地局域網可見。同樣10.xxx.xxx.xxx、從172.16.xxx.xxx 到 172.31.xxx.xxx都是私有地址,也是屬於組織內部訪問; 
169.254.xxx.xxx 屬於連接本地地址(link local IP),在單獨網段可用 
從224.xxx.xxx.xxx 到 239.xxx.xxx.xxx 屬於組播地址 
比較特殊的255.255.255.255 屬於廣播地址 
除此之外的地址就是點對點的可用的公開IPv4地址

獲取本機IP地址的正確姿勢:

public class Test{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 void main(String[] args){
      //打印本機本地地址
     System.out.println(SocketServer.getLocalHostLANAddress().toString().substring(1));
}
}



/**
* @author yins
* @date 2018年8月12日下午9:53:58
*/
package org.pdkj.web.commons.ip;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
* 獲取本地真正的IP地址,即獲得有線或者無線WiFi地址。
* 過濾虛擬機、藍牙等地址
* @author yins
* @date 2018年8月12日 下午9:53:58
*/
public class GetRealLocalIP {

/**
* 獲取本地真正的IP地址,即獲得有線或者無線WiFi地址。
* 過濾虛擬機、藍牙等地址
* @author yins
* @date 2018年8月12日下午9:56:35
* @return
*/
public static String getRealIP() {
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface
.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
.nextElement();

// 去除回環接口,子接口,未運行和接口
if (netInterface.isLoopback() || netInterface.isVirtual()
|| !netInterface.isUp()) {
continue;
}

if (!netInterface.getDisplayName().contains("Intel")
&& !netInterface.getDisplayName().contains("Realtek")) {
continue;
}
Enumeration<InetAddress> addresses = netInterface
.getInetAddresses();
System.out.println(netInterface.getDisplayName());
while (addresses.hasMoreElements()) {
InetAddress ip = addresses.nextElement();
if (ip != null) {
// ipv4
if (ip instanceof Inet4Address) {
System.out.println("ipv4 = " + ip.getHostAddress());
return ip.getHostAddress();
}
}
}
break;
}
} catch (SocketException e) {
System.err.println("Error when getting host ip address"
+ e.getMessage());
}
return null;
}
}
此代碼中只要讀取到了WiFi或者有線地址其中之一立即return。 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM