Java 獲取當(dāng)前設(shè)備的 IP 地址(最新推薦)
Internet 協(xié)議 (IP) 地址可以是連接到 TCP/IP 網(wǎng)絡(luò)的每個設(shè)備的標(biāo)識符。該標(biāo)識符用于識別和定位中間通信的節(jié)點(diǎn)。
IP 地址格式,例如 127.0.0.0,是一種人類可讀的符號。本教程演示如何使用 Java 獲取當(dāng)前機(jī)器的 IP 地址。
在 Java 中獲取當(dāng)前設(shè)備的系統(tǒng) IP 地址
IP 地址有兩個主要功能:本地尋址和主機(jī)或網(wǎng)絡(luò)接口識別。讓我們嘗試在 Java 中獲取當(dāng)前設(shè)備的系統(tǒng) IP 地址。
例子:
package Delfstack;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Get_IPAddress {
public static void main(String[] args) {
InetAddress My_IP;
try {
My_IP = InetAddress.getLocalHost();
System.out.println("The IP address of the Current Device is: " + My_IP.getHostAddress());
}
catch (UnknownHostException e) {
e.printStackTrace();
}
}
}輸出:
The IP address of the Current Device is: 172.23.96.1
運(yùn)行代碼后,我們獲得了編譯 Java 代碼的當(dāng)前設(shè)備的系統(tǒng) IP 地址。
Java中獲取當(dāng)前服務(wù)器的IP地址
獲取ip的第一反應(yīng)就是:使用InetAddress這個類:方法如下
InetAddress.getLocalHost().getHostAddress();
public static void main(String[] args) {
try {
//用 getLocalHost() 方法創(chuàng)建的InetAddress的對象
InetAddress address = InetAddress.getLocalHost();
System.out.println(address.getHostName());//主機(jī)名
System.out.println(address.getCanonicalHostName());//主機(jī)別名
System.out.println(address.getHostAddress());//獲取IP地址
System.out.println("===============");
//用域名創(chuàng)建 InetAddress對象
InetAddress address1 = InetAddress.getByName("www.wodexiangce.cn");
//獲取的是該網(wǎng)站的ip地址,如果我們所有的請求都通過nginx的,所以這里獲取到的其實(shí)是nginx服務(wù)器的IP地址
System.out.println(address1.getHostName());//www.wodexiangce.cn
System.out.println(address1.getCanonicalHostName());//124.237.121.122
System.out.println(address1.getHostAddress());//124.237.121.122
System.out.println("===============");
//用IP地址創(chuàng)建InetAddress對象
InetAddress address2 = InetAddress.getByName("220.181.111.188");
System.out.println(address2.getHostName());//220.181.111.188
System.out.println(address2.getCanonicalHostName());//220.181.111.188
System.out.println(address2.getHostAddress());//220.181.111.188
System.out.println("===============");
//根據(jù)主機(jī)名返回其可能的所有InetAddress對象
InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");
for (InetAddress addr : addresses) {
System.out.println(addr);
//www.baidu.com/220.181.111.188
//www.baidu.com/220.181.112.244
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}可以知道此時獲取到的服務(wù)器如果加了代理方式就是獲取到代理的地址,一般會使用netty代理轉(zhuǎn)發(fā)。
/**
* 獲取服務(wù)器IP地址
* @return
*/
@SuppressWarnings("unchecked")
public static String getServerIp(){
String SERVER_IP = null;
try {
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
ip = (InetAddress) ni.getInetAddresses().nextElement();
SERVER_IP = ip.getHostAddress();
if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {
SERVER_IP = ip.getHostAddress();
break;
} else {
ip = null;
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SERVER_IP;
}我的解決死方法(方法是死的,但是能解決問題^_^)
在nacos的配置里面新建一個
constant.ipHost=服務(wù)器的ip
//獲取服務(wù)器的ip
@Value("${constant.ipHost}")
private String ipHost;到此這篇關(guān)于在 Java 中獲取當(dāng)前設(shè)備的 IP 地址的文章就介紹到這了,更多相關(guān)java獲取當(dāng)前IP地址內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring代理對象導(dǎo)致的獲取不到原生對象注解的解決
本文主要介紹了Spring代理對象導(dǎo)致的獲取不到原生對象注解的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
springBoot中myBatisPlus的使用步驟及示例代碼
MyBatis-Plus 是一個 MyBatis 的增強(qiáng)工具,在 Spring Boot 項(xiàng)目里使用它能極大提升開發(fā)效率,下面為你詳細(xì)介紹在 Spring Boot 中使用 MyBatis-Plus 的步驟以及示例代碼,感興趣的朋友一起看看吧2025-03-03
SpringBoot個性化啟動Banner設(shè)置方法解析
這篇文章主要介紹了SpringBoot個性化啟動Banner設(shè)置方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
Sonar編譯問題對應(yīng):File [...] can''t be indexed twice.
今天小編就為大家分享一篇關(guān)于Sonar編譯問題對應(yīng):File [...] can't be indexed twice.,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
java實(shí)現(xiàn)的二級聯(lián)動菜單效果
這篇文章主要介紹了java實(shí)現(xiàn)的二級聯(lián)動菜單效果,結(jié)合實(shí)例形式分析了java前臺頁面布局及與后臺交互構(gòu)造聯(lián)動菜單的相關(guān)技巧,需要的朋友可以參考下2016-08-08

