Java實(shí)現(xiàn)Ip地址獲取的示例代碼
一、兩種實(shí)現(xiàn)方式
package com.lyp; import org.apache.commons.lang3.ObjectUtils; import java.net.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Optional; /** * 獲取本機(jī)IP 地址 * */ public class IpUtils { public static void main(String[] args) throws SocketException, UnknownHostException { System.out.println("傳統(tǒng)方式-----------hostAddress = " +getTraditionIp()); System.out.println( "新方式-----------hostAddress = "+IpUtils.getLocalIp4Address().get().toString().replaceAll("/","")); } /** * 傳統(tǒng)方式,非常簡(jiǎn)單直接通過(guò)InetAddress獲取,但不準(zhǔn)確獲取的為虛擬ip * @throws UnknownHostException */ public static String getTraditionIp() throws UnknownHostException { InetAddress localHost = InetAddress.getLocalHost(); String hostAddress = localHost.getHostAddress(); String hostName = localHost.getHostName(); return hostAddress; } /* * 獲取本機(jī)所有網(wǎng)卡信息 得到所有IP信息 * @return Inet4Address> */ public static List<Inet4Address> getLocalIp4AddressFromNetworkInterface() throws SocketException { List<Inet4Address> addresses = new ArrayList<>(1); // 所有網(wǎng)絡(luò)接口信息 Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); if (ObjectUtils.isEmpty(networkInterfaces)) { return addresses; } while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); //濾回環(huán)網(wǎng)卡、點(diǎn)對(duì)點(diǎn)網(wǎng)卡、非活動(dòng)網(wǎng)卡、虛擬網(wǎng)卡并要求網(wǎng)卡名字是eth或ens開(kāi)頭 if (!isValidInterface(networkInterface)) { continue; } // 所有網(wǎng)絡(luò)接口的IP地址信息 Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); // 判斷是否是IPv4,并且內(nèi)網(wǎng)地址并過(guò)濾回環(huán)地址. if (isValidAddress(inetAddress)) { addresses.add((Inet4Address) inetAddress); } } } return addresses; } /** * 過(guò)濾回環(huán)網(wǎng)卡、點(diǎn)對(duì)點(diǎn)網(wǎng)卡、非活動(dòng)網(wǎng)卡、虛擬網(wǎng)卡并要求網(wǎng)卡名字是eth或ens開(kāi)頭 * * @param ni 網(wǎng)卡 * @return 如果滿足要求則true,否則false */ private static boolean isValidInterface(NetworkInterface ni) throws SocketException { return !ni.isLoopback() && !ni.isPointToPoint() && ni.isUp() && !ni.isVirtual() && (ni.getName().startsWith("eth") || ni.getName().startsWith("ens")); } /** * 判斷是否是IPv4,并且內(nèi)網(wǎng)地址并過(guò)濾回環(huán)地址. */ private static boolean isValidAddress(InetAddress address) { return address instanceof Inet4Address && address.isSiteLocalAddress() && !address.isLoopbackAddress(); } /* * 通過(guò)Socket 唯一確定一個(gè)IP * 當(dāng)有多個(gè)網(wǎng)卡的時(shí)候,使用這種方式一般都可以得到想要的IP。甚至不要求外網(wǎng)地址8.8.8.8是可連通的 * @return Inet4Address> */ private static Optional<Inet4Address> getIpBySocket() throws SocketException { try (final DatagramSocket socket = new DatagramSocket()) { socket.connect(InetAddress.getByName("8.8.8.8"), 10002); if (socket.getLocalAddress() instanceof Inet4Address) { return Optional.of((Inet4Address) socket.getLocalAddress()); } } catch (UnknownHostException networkInterfaces) { throw new RuntimeException(networkInterfaces); } return Optional.empty(); } /* * 獲取本地IPv4地址 * @return Inet4Address> */ public static Optional<Inet4Address> getLocalIp4Address() throws SocketException { final List<Inet4Address> inet4Addresses = getLocalIp4AddressFromNetworkInterface(); if (inet4Addresses.size() != 1) { final Optional<Inet4Address> ipBySocketOpt = getIpBySocket(); if (ipBySocketOpt.isPresent()) { return ipBySocketOpt; } else { return inet4Addresses.isEmpty() ? Optional.empty() : Optional.of(inet4Addresses.get(0)); } } return Optional.of(inet4Addresses.get(0)); } }
二、測(cè)試結(jié)果
到此這篇關(guān)于Java實(shí)現(xiàn)Ip地址獲取的示例代碼的文章就介紹到這了,更多相關(guān)Java獲取Ip地址內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis關(guān)聯(lián)查詢結(jié)果集對(duì)象嵌套的具體使用
在查詢時(shí)經(jīng)常出現(xiàn)一對(duì)多”的關(guān)系,所有會(huì)出現(xiàn)嵌套對(duì)象的情況,本文主要介紹了Mybatis關(guān)聯(lián)查詢結(jié)果集對(duì)象嵌套的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02詳解Java的MyBatis框架中的緩存與緩存的使用改進(jìn)
很多人在使用MyBatis的緩存后經(jīng)常會(huì)遇到MySQL分頁(yè)查詢的顯示問(wèn)題,針對(duì)于此,這里我們就來(lái)詳解Java的MyBatis框架中的緩存與緩存的使用改進(jìn),首先來(lái)回顧一下MyBatis的緩存機(jī)制與執(zhí)行:2016-06-06java.lang.UnsupportedClassVersionError錯(cuò)誤的解決辦法(附圖文)
這篇文章主要給大家介紹了關(guān)于java.lang.UnsupportedClassVersionError錯(cuò)誤的解決辦法,"java.lang.UnsupportedClassVersionError"意味著您正在運(yùn)行的Java版本與編譯該類時(shí)使用的Java版本不兼容,需要的朋友可以參考下2023-10-10java LRU(Least Recently Used )詳解及實(shí)例代碼
這篇文章主要介紹了java LRU(Least Recently Used )詳解及實(shí)例代碼的相關(guān)資料,Java里面實(shí)現(xiàn)LRU緩存通常有兩種選擇,一種是使用LinkedHashMap,一種是自己設(shè)計(jì)數(shù)據(jù)結(jié)構(gòu),使用鏈表+HashMap,需要的朋友可以參考下2016-11-11springboot整合RabbitMQ發(fā)送短信的實(shí)現(xiàn)
本文會(huì)和SpringBoot做整合,實(shí)現(xiàn)RabbitMQ發(fā)送短信,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05SpringBoot?緩存預(yù)熱的實(shí)現(xiàn)
本文主要介紹了SpringBoot?緩存預(yù)熱的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2007-11-11MyBatisPlus+Spring實(shí)現(xiàn)聲明式事務(wù)的方法實(shí)現(xiàn)
本文主要介紹了MyBatisPlus+Spring實(shí)現(xiàn)聲明式事務(wù)的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07java使用TimerTask定時(shí)器獲取指定網(wǎng)絡(luò)數(shù)據(jù)
java.util.Timer定時(shí)器,實(shí)際上是個(gè)線程,定時(shí)調(diào)度所擁有的TimerTasks。一個(gè)TimerTask實(shí)際上就是一個(gè)擁有run方法的類,需要定時(shí)執(zhí)行的代碼放到run方法體內(nèi),TimerTask一般是以匿名類的方式創(chuàng)建,下面的就用示例來(lái)學(xué)習(xí)他的使用方法2014-01-01