欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java獲取本機IP地址的方法代碼示例(內(nèi)網(wǎng)、公網(wǎng))

 更新時間:2024年07月23日 10:28:57   作者:EIL_XU_  
在IT領(lǐng)域獲取本機IP地址是一項基礎(chǔ)但重要的任務,特別是在網(wǎng)絡編程、遠程協(xié)作和設(shè)備通信中,這篇文章主要給大家介紹了關(guān)于Java獲取本機IP地址的方法(內(nèi)網(wǎng)、公網(wǎng)),需要的朋友可以參考下

起因是公司一個springboot項目啟動類打印了本機IP地址加端口號,方便訪問項目頁面,但是發(fā)現(xiàn)打印出來的不是“無線局域網(wǎng)”的ip而是“以太網(wǎng)適配器”ip,如下圖所示

這樣就導致后續(xù)本地起項目連接xxl-job注冊節(jié)點的時候因為不在同個局域網(wǎng)下ping不通出問題,所以需要解決一下。

一、直接獲取本機IP(會受到虛擬機干擾)

public static String getInterIP1() throws Exception {
    return InetAddress.getLocalHost().getHostAddress();
}

二、獲取本機IP(排除虛擬機干擾)

public static InetAddress getLocalHostLANAddress() throws UnknownHostException {
        try {
            InetAddress candidateAddress = null;
            // 遍歷所有的網(wǎng)絡接口
            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類型的地址未被發(fā)現(xiàn),先記錄候選地址
                            candidateAddress = inetAddr;
                        }
                    }
                }
            }
            if (candidateAddress != null) {
                return candidateAddress;
            }
            // 如果沒有發(fā)現(xiàn) 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;
        }
    }

三、獲取本機公網(wǎng)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("獲取本機公網(wǎng)ip: " + getOutIP());
        }

        public static String getInterIP1() throws Exception {
            return InetAddress.getLocalHost().getHostAddress();
        }

    public static InetAddress getLocalHostLANAddress() throws UnknownHostException {
        try {
            InetAddress candidateAddress = null;
            // 遍歷所有的網(wǎng)絡接口
            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類型的地址未被發(fā)現(xiàn),先記錄候選地址
                            candidateAddress = inetAddr;
                        }
                    }
                }
            }
            if (candidateAddress != null) {
                return candidateAddress;
            }
            // 如果沒有發(fā)現(xiàn) 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
獲取本機公網(wǎng)ip: 14.145.43.147

這里打印出來的便對應上前言命令行截圖里的ip信息,然后公網(wǎng)ip對應的是百度搜索ip查到的公網(wǎng)ip地址 

五、手動禁用虛擬機排除干擾

除了用代碼邏輯排除虛擬機干擾,我們也可以直接禁用電腦的虛擬機適配器。

步驟:

  • win+R 
  • devmgmt.msc 打開設(shè)備管理器
  • 找到網(wǎng)絡適配器-Hyper-V Virtual 開頭的  右鍵禁用

此時win+R cmd 輸入ipconfig,可以看到已經(jīng)沒有虛擬機ip了

此時java使用

InetAddress.getLocalHost().getHostAddress()

也可以獲取到192.168開頭的wifi地址了

總結(jié)

到此這篇關(guān)于Java獲取本機IP地址的文章就介紹到這了,更多相關(guān)Java獲取本機IP地址內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot全局異常處理方式@ControllerAdvice和@ExceptionHandler

    springboot全局異常處理方式@ControllerAdvice和@ExceptionHandler

    文章總結(jié)了個人在處理全局異常處理時的經(jīng)驗,包括使用`StatusEnum`來定義狀態(tài)碼,旨在為讀者提供參考,并鼓勵大家支持腳本之家
    2024-11-11
  • spring?boot學習筆記之操作ActiveMQ指南

    spring?boot學習筆記之操作ActiveMQ指南

    ActiveMQ是一種開源的基于JMS規(guī)范的一種消息中間件的實現(xiàn),ActiveMQ的設(shè)計目標是提供標準的,面向消息的,能夠跨越多語言和多系統(tǒng)的應用集成消息通信中間件,這篇文章主要給大家介紹了關(guān)于spring?boot學習筆記之操作ActiveMQ指南的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • java利用多線程和Socket實現(xiàn)猜拳游戲

    java利用多線程和Socket實現(xiàn)猜拳游戲

    這篇文章主要為大家詳細介紹了java利用多線程和Socket實現(xiàn)猜拳游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • 用java實現(xiàn)楊輝三角的示例代碼

    用java實現(xiàn)楊輝三角的示例代碼

    本篇文章主要介紹了用java實現(xiàn)楊輝三角的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • JPA如何設(shè)置表名和實體名,表字段與實體字段的對應

    JPA如何設(shè)置表名和實體名,表字段與實體字段的對應

    這篇文章主要介紹了JPA如何設(shè)置表名和實體名,表字段與實體字段的對應,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Jmeter生成UUID作為唯一標識符過程圖解

    Jmeter生成UUID作為唯一標識符過程圖解

    這篇文章主要介紹了Jmeter生成UUID作為唯一標識符過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • SpringBoot如何使用TestEntityManager進行JPA集成測試

    SpringBoot如何使用TestEntityManager進行JPA集成測試

    TestEntityManager是Spring Framework提供的一個測試框架,它可以幫助我們進行 JPA 集成測試,在本文中,我們將介紹如何使用 TestEntityManager 進行 JPA 集成測試,感興趣的跟著小編一起來學習吧
    2023-06-06
  • java中對字符串每個字符統(tǒng)計的方法

    java中對字符串每個字符統(tǒng)計的方法

    java中對字符串每個字符統(tǒng)計的方法,需要的朋友可以參考一下
    2013-03-03
  • Spring Boot整合MyBatis-Plus實現(xiàn)CRUD操作的示例代碼

    Spring Boot整合MyBatis-Plus實現(xiàn)CRUD操作的示例代碼

    本文主要介紹了Spring Boot整合MyBatis-Plus實現(xiàn)CRUD操作,可以快速實現(xiàn)數(shù)據(jù)庫的增刪改查操作,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧
    2025-04-04
  • 非常適合新手學生的Java線程池超詳細分析

    非常適合新手學生的Java線程池超詳細分析

    作者是一個來自河源的大三在校生,以下筆記都是作者自學之路的一些淺薄經(jīng)驗,如有錯誤請指正,將來會不斷的完善筆記,幫助更多的Java愛好者入門
    2022-03-03

最新評論