獲取本機的ip地址(排除虛擬機,藍牙等ip)
阿新 • • 發佈:2018-09-02
運行 exception play pla 過濾 contains network localhost static
項目中遇到了要獲取本地ip的需求,網上查找資料遇到很多坑,很多Java獲取本機ip地址的方法要麽是根本獲取不到,要麽是獲取的有問題。
網上常見的方法如下
InetAddress.getLocalHost().getHostAddress()
但是如果電腦裏面有Lan,WIFI,藍牙熱點,虛擬機網卡,即存在很多的網絡接口(network interfaces),每個網絡接口就包含一個IP地址,並不是所有的IP地址能被外部或局域網訪問,比如說虛擬機網卡地址等等。上面獲取到的ip就會有誤。
下面是正確的獲取ip地址的方法,親測無誤:
方法一:
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.UnknownHostException; import java.util.Enumeration; public class getRealIp { public static void main(String[] args) { try { // 正確的IP拿法 System.out.println("get LocalHost LAN Address : " + getLocalHostLANAddress().getHostAddress()); } catch (UnknownHostException e) { e.printStackTrace(); } } // 正確的IP拿法,即優先拿site-local地址 private 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; } } }
參考博客地址:https://blog.csdn.net/u011809209/article/details/77236602
這裏插上一句,上面的方法之前在java項目中能準確獲取到ip,但是放在項目中之後,又不能獲取準確的ip,也是很迷,然後又另外找了一種方法,如下:
方法二:
import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; /** * 獲取本地真正的IP地址,即獲得有線或者無線WiFi地址。 * 過濾虛擬機、藍牙等地址 */ public class getRealLocalIP { 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; } }
參考博客地址:https://blog.csdn.net/yinshuomail/article/details/81624648
總之網上比較靠譜的方法就找到這兩種,感謝其他博主的博客。
獲取本機的ip地址(排除虛擬機,藍牙等ip)