![logo](https://i.iter01.com/images/f31631aedc6dfdfde373a4f2098c08c219b5a69e71e2fcea1f1f44bab882fdd2.png)
Java實現獲取本機Ip的工具類
獲取本機Ip算是比較常見的一個需求場景了,比如業務報警,可能就會帶上出問題的機器IP,方便直接上去看日誌定位問題,那麼問題來了,如何獲取機器IP呢?
I. IpUtil工具類
1. 基本方法
如何獲取機器Ip?如果瞭解InetAddress這個工具類,就很容易寫出一個簡單的工具類,如下
public static String getLocalIP() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
複製程式碼
上面的實現有問題麼?
當然沒問題,拿我本機和阿里伺服器執行一下,並沒有問題如實的輸出了預期的IP
本機執行後截圖如下:
![本機](https://i.iter01.com/images/bc1664eb5b53bf0241c51e3c27964a02915ba9950a1c2e9de64bca49e6a8131b.png)
阿里雲機器執行後截圖如下:
![阿里雲](https://i.iter01.com/images/771686a007c10f455906d551406cc498268dadb6c20f3a62adba40e81bcf9e9e.png)
再問一句,那是否就真的沒有問題了呢?
- 在某些情況下,可能返回的是
127.0.0.1
在虛擬機器中執行時,就可能遇到這個問題,截圖如下
![虛擬機器](https://i.iter01.com/images/07c8d9d387b2a7606e6bc96ef3b8b9a47b4c3618d43be0e1facb3cfeeb24302f.png)
2. 進階版
做一點簡單的改動,獲取IpV4的地址,原始碼如下
/**
* 直接根據第一個網路卡地址作為其內網ipv4地址,避免返回 127.0.0.1
*
* @return
*/
public static String getLocalIpByNetcard() {
try {
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) {
NetworkInterface item = e.nextElement();
for (InterfaceAddress address : item.getInterfaceAddresses()) {
if (item.isLoopback() || !item.isUp()) {
continue;
}
if (address.getAddress() instanceof Inet4Address) {
Inet4Address inet4Address = (Inet4Address) address.getAddress();
return inet4Address.getHostAddress();
}
}
}
return InetAddress.getLocalHost().getHostAddress();
} catch (SocketException | UnknownHostException e) {
throw new RuntimeException(e);
}
}
複製程式碼
再次測試,輸出如下
![虛擬機器](https://i.iter01.com/images/c0ffe9288d0cec1cc3db6c84c3e44e1d3bc73bad94e7c1ac370a7f51d669b79b.png)
3. 完整工具類
import java.net.*;
import java.util.Enumeration;
public class IpUtil {
public static final String DEFAULT_IP = "127.0.0.1";
/**
* 直接根據第一個網路卡地址作為其內網ipv4地址,避免返回 127.0.0.1
*
* @return
*/
public static String getLocalIpByNetcard() {
try {
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) {
NetworkInterface item = e.nextElement();
for (InterfaceAddress address : item.getInterfaceAddresses()) {
if (item.isLoopback() || !item.isUp()) {
continue;
}
if (address.getAddress() instanceof Inet4Address) {
Inet4Address inet4Address = (Inet4Address) address.getAddress();
return inet4Address.getHostAddress();
}
}
}
return InetAddress.getLocalHost().getHostAddress();
} catch (SocketException | UnknownHostException e) {
throw new RuntimeException(e);
}
}
public static String getLocalIP() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
}
複製程式碼
II. 其他
1. 一灰灰Blog: https://liuyueyi.github.io/hexblog
一灰灰的個人部落格,記錄所有學習和工作中的博文,歡迎大家前去逛逛
2. 宣告
盡信書則不如,已上內容,純屬一家之言,因個人能力有限,難免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激
- 微博地址: 小灰灰Blog
- QQ: 一灰灰/3302797840
3. 掃描關注
![QrCode](https://i.iter01.com/images/3400c2259d835156b462231f084b6a9d812b7d96d2ff41d4d1c028280dc44cc9.jpg)