Java獲取電腦真實(shí)IP地址的示例代碼
/** * @author yins * @date 2018年8月12日下午9:53:58 */import java.net.Inet4Address;import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.util.Enumeration; /** * 獲取本地真正的IP地址,即獲得有線(xiàn)或者無(wú)線(xiàn)WiFi地址。 * 過(guò)濾虛擬機(jī)、藍(lán)牙等地址 * @author yins * @date 2018年8月12日 下午9:53:58 */public class GetRealLocalIP { /** * 獲取本地真正的IP地址,即獲得有線(xiàn)或者無(wú)線(xiàn)WiFi地址。 * 過(guò)濾虛擬機(jī)、藍(lán)牙等地址 * @author yins * @date 2018年8月12日下午9:56:35 * @return */ public static String getRealIP() { try { Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface .getNetworkInterfaces(); while (allNetInterfaces.hasMoreElements()) {NetworkInterface netInterface = (NetworkInterface) allNetInterfaces .nextElement(); // 去除回環(huán)接口,子接口,未運(yùn)行和接口if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) { continue;}if (!netInterface.getDisplayName().contains('Intel') && !netInterface.getDisplayName().contains('Realtek')) { continue;}Enumeration<InetAddress> addresses = netInterface .getInetAddresses();System.out.println(netInterface.getDisplayName());while (addresses.hasMoreElements()) { InetAddress ip = addresses.nextElement(); if (ip != null) { // ipv4 if (ip instanceof Inet4Address) { System.out.println('ipv4 = ' + ip.getHostAddress()); return ip.getHostAddress(); } }}break; } } catch (SocketException e) { System.err.println('Error when getting host ip address' + e.getMessage()); } return null; }}
此代碼中只要讀取到了WiFi或者有線(xiàn)地址其中之一立即return。
以上就是Java獲取電腦真實(shí)IP地址的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java獲取IP地址的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. JAMon(Java Application Monitor)備忘記2. python 編寫(xiě)輸出到csv的操作3. Python PyQt5-圖形界面的美化操作4. 利用原生JS實(shí)現(xiàn)歡樂(lè)水果機(jī)小游戲5. js的一些潛在規(guī)則使用分析6. PHP swoole的process模塊創(chuàng)建和使用子進(jìn)程操作示例7. 詳解python第三方庫(kù)的安裝、PyInstaller庫(kù)、random庫(kù)8. 手把手教你用python發(fā)送短消息(基于阿里云平臺(tái))9. Python PyQt5中彈出子窗口解決子窗口一閃而過(guò)的問(wèn)題10. ASP.Net MVC利用NPOI導(dǎo)入導(dǎo)出Excel的示例代碼
