[Java] java 使用 java.net.InterfaceAddress 获取网卡信息 →→→→→进入此内容的聊天室

来自 , 2020-04-21, 写在 Java, 查看 112 次.
URL http://www.code666.cn/view/934815ad
  1. import java.net.InterfaceAddress;
  2. import java.net.NetworkInterface;
  3. import java.net.SocketException;
  4. import java.util.Enumeration;
  5. import java.util.Iterator;
  6. import java.util.List;
  7.  
  8. public class NetworkParameterDemo {
  9.   public static void main(String[] args) throws Exception {
  10.     Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
  11.     while (en.hasMoreElements()) {
  12.       NetworkInterface ni = en.nextElement();
  13.       printParameter(ni);
  14.  
  15.     }
  16.   }
  17.  
  18.   public static void printParameter(NetworkInterface ni) throws SocketException {
  19.     System.out.println(\" Name = \" + ni.getName());
  20.    System.out.println(\" Display Name = \" + ni.getDisplayName());
  21.    System.out.println(\" Is up = \" + ni.isUp());
  22.    System.out.println(\" Support multicast = \" + ni.supportsMulticast());
  23.    System.out.println(\" Is loopback = \" + ni.isLoopback());
  24.    System.out.println(\" Is virtual = \" + ni.isVirtual());
  25.    System.out.println(\" Is point to point = \" + ni.isPointToPoint());
  26.    System.out.println(\" Hardware address = \" + ni.getHardwareAddress());
  27.    System.out.println(\" MTU = \" + ni.getMTU());
  28.  
  29.    System.out.println(\"\\nList of Interface Addresses:\");
  30.    List<InterfaceAddress> list = ni.getInterfaceAddresses();
  31.    Iterator<InterfaceAddress> it = list.iterator();
  32.  
  33.    while (it.hasNext()) {
  34.      InterfaceAddress ia = it.next();
  35.      System.out.println(\" Address = \" + ia.getAddress());
  36.      System.out.println(\" Broadcast = \" + ia.getBroadcast());
  37.      System.out.println(\" Network prefix length = \" + ia.getNetworkPrefixLength());
  38.      System.out.println(\"\");
  39.    }
  40.  }
  41. }
  42. //该片段来自于http://yuncode.net
  43.  

回复 "java 使用 java.net.InterfaceAddress 获取网卡信息"

这儿你可以回复上面这条便签

captcha