java中通過網(wǎng)卡名稱獲取IP地址
package me.xuzs.sso.test;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class InternetTest {
public static void main(String[] args) {
String netCard = "lo";
try {
Enumeration<NetworkInterface> netInterfaces = NetworkInterface
.getNetworkInterfaces();
if (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = netInterfaces.nextElement();
if (netCard.equals(netInterface.getName())) {
// 子接口,linux下會取到父接口??
Enumeration<NetworkInterface> subnetInterfaces = netInterface
.getSubInterfaces();
while (subnetInterfaces.hasMoreElements()) {
NetworkInterface subnetInterface = subnetInterfaces
.nextElement();
System.out.println(subnetInterface.getName());
Enumeration<InetAddress> subaddresses = netInterface
.getInetAddresses();
while (subaddresses.hasMoreElements()) {
InetAddress subaddress = subaddresses.nextElement();
System.out.println(subaddress.getHostAddress());
}
}
// 打印接口下所有IP
System.out.println(netInterface.getName());
Enumeration<InetAddress> addresses = netInterface
.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
System.out.println(address.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
相關(guān)文章
簡單談?wù)刯ava的異常處理(Try Catch Finally)
在程序設(shè)計中,進行異常處理是非常關(guān)鍵和重要的一部分。一個程序的異常處理框架的好壞直接影響到整個項目的代碼質(zhì)量以及后期維護成本和難度。2016-03-03Spring?Boot?使用?Hutool-jwt?實現(xiàn)?token?驗證功能
JWT?就是一種網(wǎng)絡(luò)身份認證和信息交換格式,這篇文章主要介紹了Spring Boot使用Hutool-jwt實現(xiàn)token驗證,需要的朋友可以參考下2023-07-07Intellij Idea中批量導入第三方j(luò)ar包的全過程
引入jar包一般都是針對小的java項目,這篇文章主要給大家介紹了關(guān)于Intellij Idea中批量導入第三方j(luò)ar包的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2021-10-10Java Hutool 包工具類推薦 ExcelUtil詳解
這篇文章主要介紹了Java Hutool 包工具類推薦 ExcelUtil詳解,需要引入hutool包,版本號可根據(jù)實際情況更換,除hutool包之外,還需要引入操作Excel必要包,本文給大家介紹的非常詳細,需要的朋友可以參考下2022-09-09