源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

java中通过网卡名称获取IP地址

  • 时间:2022-11-28 00:43 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:java中通过网卡名称获取IP地址
[u]复制代码[/u] 代码如下:
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();         }     } }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部