亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

這樣就導(dǎo)致后續(xù)本地起項(xiàng)目連接xxl-job注冊(cè)節(jié)點(diǎn)的時(shí)候因?yàn)椴辉谕瑐€(gè)局域網(wǎng)下ping不通出問題,所以需要解決一下。

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

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

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

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;
        }
    }

三、獲取本機(jī)公網(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 "";
}

四、測(cè)試

public class IpTest {

        public static void main(String[] args) throws Exception {
            System.out.println("獲取本機(jī)ip: " + getInterIP1());
//            System.out.println("getInterIP2: " + getInterIP2());
            System.out.println("獲取本機(jī)ip(排除虛擬機(jī)干擾): " + String.valueOf(getLocalHostLANAddress()).substring(1));
//            System.out.println("getOutIPV4: " + getOutIPV4());
            System.out.println("獲取本機(jī)公網(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 "";
        }

}
獲取本機(jī)ip: 172.17.16.1
獲取本機(jī)ip(排除虛擬機(jī)干擾): 192.168.100.100
獲取本機(jī)公網(wǎng)ip: 14.145.43.147

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

五、手動(dòng)禁用虛擬機(jī)排除干擾

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

步驟:

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

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

此時(shí)java使用

InetAddress.getLocalHost().getHostAddress()

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

總結(jié)

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

相關(guān)文章

最新評(píng)論