获取网卡IP与MAC地址
2012-07-12 11:54
295 查看
import java.net.Inet4Address; import java.net.InetAddress; import java.net.InterfaceAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; public class GetSystemInfoUtil { // 获取ip地址 public static List<String> GetNetworkCardIPAddress() throws SocketException { List<String> allNetworkCardIPAddresses = new ArrayList<String>(); Enumeration<NetworkInterface> allNetworkInterfaces = NetworkInterface .getNetworkInterfaces(); while (allNetworkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = allNetworkInterfaces .nextElement(); List<InterfaceAddress> interfaceAddresses = networkInterface .getInterfaceAddresses(); for (InterfaceAddress address : interfaceAddresses) { InetAddress addressTemp = address.getAddress(); if (addressTemp instanceof Inet4Address && !addressTemp.getHostAddress().equals("127.0.0.1")) { allNetworkCardIPAddresses.add(addressTemp.getHostAddress()); } } } return allNetworkCardIPAddresses; } // 获取mac地址 public static List<String> GetNetWorkCardMacAddress() throws SocketException { List<String> allNetworkCardIPAddresses = new ArrayList<String>(); Enumeration<NetworkInterface> allNetworkInterfaces = NetworkInterface .getNetworkInterfaces(); while (allNetworkInterfaces.hasMoreElements()) { StringBuilder sb = new StringBuilder(); NetworkInterface networkInterface = allNetworkInterfaces .nextElement(); if (networkInterface.isPointToPoint()) continue; if (networkInterface.isLoopback()) continue; byte[] hardwareAddress = networkInterface.getHardwareAddress(); if (hardwareAddress == null) continue; for (byte b : hardwareAddress) { String temp = Integer.toHexString(b); int length = temp.length(); if (length < 2) { sb.append("0" + temp + "-"); continue; } sb.append(temp.substring(length - 2)); sb.append("-"); } sb.deleteCharAt(sb.length() - 1); allNetworkCardIPAddresses.add(sb.toString()); } return allNetworkCardIPAddresses; } public static void main(String[] args) throws Exception { List<String> getNetworkCardIPAddress = GetNetworkCardIPAddress(); for (String str : getNetworkCardIPAddress) { System.err.println(str); } List<String> getNetWorkCardMacAddress = GetNetWorkCardMacAddress(); for (String str : getNetWorkCardMacAddress) { System.err.println(str); } } }
相关文章推荐
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)
- 获取网卡地址 mac or ip or net mask
- VB获取本机网卡类型、IP、MAC地址
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)
- linux 下C实现获取网卡IP和MAC地址
- 通过当前IP获取当前网卡的MAC地址
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)
- Android获取手机地址(Mac、ip和imei)
- Android获取Ethernet、WIFI的ip和mac地址
- 获取硬盘ID信息及网卡MAC地址的代码
- ASP.NET获取IP与MAC地址的方法
- 通过IP获取MAC地址例子(应用层)
- MFC 获取本地电脑的IP和MAC地址
- 【C++】获得本机所有网卡的IP和MAC地址信息
- 一分钟学会获取本机网卡物理地址(MAC)
- java服务器端根据ip获取客户端mac地址
- linux 设置网络API --- 开/关网卡、设置/获取Mac地址
- 获取网卡的硬件地址 和ip 地址
- JS获取客户端网卡物理地址(MAC)
- 如何获取客户端的IP与MAC地址