最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

這樣就導(dǎo)致后續(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)絡(luò)接口
            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)絡(luò)接口
            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

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

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

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

步驟:

  • win+R 
  • devmgmt.msc 打開設(shè)備管理器
  • 找到網(wǎng)絡(luò)適配器-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)文章

  • Java使用JDBC實現(xiàn)Oracle用戶認證的方法詳解

    Java使用JDBC實現(xiàn)Oracle用戶認證的方法詳解

    這篇文章主要介紹了Java使用JDBC實現(xiàn)Oracle用戶認證的方法,結(jié)合實例形式分析了java使用jdbc實現(xiàn)數(shù)據(jù)庫連接、建表、添加用戶、用戶認證等操作流程與相關(guān)注意事項,需要的朋友可以參考下
    2017-08-08
  • java 對數(shù)和指數(shù)計算方式

    java 對數(shù)和指數(shù)計算方式

    這篇文章主要介紹了java 對數(shù)和指數(shù)計算方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring IoC和DI深度解析

    Spring IoC和DI深度解析

    Spring是包含了眾多工具方法的IoC容器,本文給大家介紹Spring IoC和DI深度解析,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,感興趣的朋友跟隨小編一起看看吧
    2026-02-02
  • 一文搞懂Java創(chuàng)建線程的五種方法

    一文搞懂Java創(chuàng)建線程的五種方法

    本文主要為大家詳細介紹一下Java實現(xiàn)線程創(chuàng)建的五種常見方式,文中的示例代碼講解詳細,對我們學(xué)習(xí)有一定的幫助,感興趣的可以跟隨小編學(xué)習(xí)一下
    2022-06-06
  • 關(guān)于IDEA2020.1新建項目maven PKIX 報錯問題解決方法

    關(guān)于IDEA2020.1新建項目maven PKIX 報錯問題解決方法

    這篇文章主要介紹了關(guān)于IDEA2020.1新建項目maven PKIX 報錯問題解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • log4j日志格式加入自定義字段信息方式

    log4j日志格式加入自定義字段信息方式

    這篇文章主要介紹了log4j日志格式加入自定義字段信息方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring之Environment類的使用方式

    Spring之Environment類的使用方式

    這篇文章主要介紹了Spring之Environment類的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 基于Java編寫串口通信工具

    基于Java編寫串口通信工具

    這篇文章主要為大家詳細介紹了基于Java編寫的一個帶有圖形界面的簡單串口通信工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • MybatisPlus之likeRight的用法

    MybatisPlus之likeRight的用法

    這篇文章主要介紹了MybatisPlus之likeRight的用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 分布式Netty源碼分析概覽

    分布式Netty源碼分析概覽

    這篇文章主要為大家介紹了分布式Netty源碼分析概覽,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03

最新評論

彭州市| 上饶市| 咸宁市| 通江县| 延长县| 昭通市| 镇原县| 汽车| 安陆市| 资源县| 沁阳市| 临湘市| 海门市| 南宫市| 贵州省| 青神县| 阿巴嘎旗| 庆城县| 苗栗市| 德庆县| 岢岚县| 杭州市| 分宜县| 西吉县| 济南市| 大姚县| 绿春县| 宜宾市| 水城县| 昌宁县| 湾仔区| 米林县| 宣化县| 扬州市| 颍上县| 龙江县| 平罗县| 三亚市| 靖江市| 本溪| 西充县|