Java 获取本机IP和Mac以及网卡信息

本文介绍了如何在Java中获取局域网IP、MAC地址以及网卡信息。提供的解决方案适用于Android、Windows和Linux系统,覆盖了大部分设备,但不支持Android 2.2。同时分享了Android平台上获取这些信息的代码,并提供了Java架构学习交流群的信息。
摘要由CSDN通过智能技术生成

1.获取局域网ip和mac(如果电脑没有直接连接外网),否则获取公网ip

2.通过第三放获取公网ip

public class NetworkUtils {

	/**
	 * 获取本地IP列表(针对多网卡情况)
	 * @return
	 */
	public static Map<String, Object> getLocalInetMac() {

		Map<String, Object> ipMacInfo = null;
		try {
			Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
					.getNetworkInterfaces();
			while (networkInterfaces.hasMoreElements()) {
				NetworkInterface networkInterface = networkInterfaces
						.nextElement();
				Enumeration<InetAddress> inetAddresses = networkInterface
						.getInetAddresses();

				while (inetAddresses.hasMoreElements()) {
					InetAddress inetAddress = inetAddresses.nextElement();
					ipMacInfo = pickInetAddress(inetAddress, networkInterface);
					if (ipMacInfo != null) {
					        Log.e("IP-MAC-1",ipMacInfo );
						return ipMacInfo;
					}
				}
			}
		} catch (SocketException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static Map<String, Object> pickInetAddress(InetAddress inetAddress,
			NetworkInterface ni) {
		try {
			String name = ni.getDisplayName();
			if (name.contains("Adapter")
					|| name.contains("Virtual") || name.contains("VMnet") || name.contains("#")) {
				return null;
			}
			if (ni.isVirtual() || !ni.isUp() || !ni.supportsMulticast()) {
				return null;
			}

			if (inetAddress.isSiteLocalAddress()) {
				Formatter formatter = new Formatter();
				String sMAC = null;
				byte[] macBuf = ni.getHardwareAddress();
				for (int i = 0; i < macBuf.length; i++) {
					sMAC = formatter.format(Locale.getDefault(), "%02X%s",
							macBuf[i], (i < macBuf.length - 1) ? "-" : "")
							.toString();
				}
				formatter.close();
				Map<String, Object> ipMacInfo = new HashMap<String, Object>();
				ipMacInfo.put("hostname", inetAddress.getHostName()); //系统当前hostname
				ipMacInfo.put("ip", inetAddress.getHostAddress()); //ip地址
				ipMacInfo.put("ipnet&#
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java获取客户端的MAC地址可以通过以下步骤实现: 1. 导入相关的Java类库:需要导入`java.net`包和`java.net.NetworkInterface`类。 2. 使用`NetworkInterface`类获取本机的网络接口列表,可以通过`NetworkInterface.getNetworkInterfaces()`方法实现。这个方法将返回一个枚举类型的网络接口,包含了所有的网络接口,如以太网卡、无线网卡等。 3. 遍历网络接口列表,获取本机MAC地址。可以通过调用`NetworkInterface.getHardwareAddress()`方法来获取MAC地址的字节数组,之后将字节数组转换为十六进制字符串。 下面是利用Java代码实现的示例: ``` import java.net.*; import java.util.*; public class GetMacAddress { public static void main(String[] args) { try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); byte[] mac = networkInterface.getHardwareAddress(); if (mac != null) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); } System.out.println("MAC地址为:" + sb.toString()); } } } catch (SocketException e) { e.printStackTrace(); } } } ``` 以上代码会遍历本机的所有网络接口,获取到对应的MAC地址,并将其以十六进制字符串的形式输出。注意,可能会有多个网络接口,因此可能会输出多个MAC地址。 ### 回答2: 要使用Java获取客户端的MAC地址,可以使用Java的网络编程库和系统命令。 一种方法是使用Java的网络编程库来获取客户端的IP地址,然后使用系统命令来获取与该IP地址关联的MAC地址。 首先,可以通过Java的InetAddress类的getLocalHost()方法获取本地IP地址。然后,通过使用NetworkInterface类的getByInetAddress()方法,传入上一步获取IP地址,来获取与该IP地址关联的网络接口。 接下来,通过调用NetworkInterface类的getHardwareAddress()方法,即可获取MAC地址的字节数组。然后,可以将字节数组转换为十六进制字符串。 下面是示例代码: ```java import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; public class GetMacAddress { public static void main(String[] args) { try { InetAddress ip = InetAddress.getLocalHost(); NetworkInterface network = NetworkInterface.getByInetAddress(ip); byte[] mac = network.getHardwareAddress(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); } System.out.println("MAC Address: " + sb.toString()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (SocketException e) { e.printStackTrace(); } } } ``` 注意,此示例中只是获取本地主机的MAC地址。如果要获取远程主机的MAC地址,需要使用不同的方法。 另外,获取MAC地址需要在有相应权限的情况下才能成功运行,特别是在某些操作系统中可能需要管理员权限。 ### 回答3: 在Java获取客户端的MAC地址的方式有两种:使用ARP命令或通过网络接口获取。 第一种方法是使用ARP命令。在Windows系统上,可以通过使用Java的Runtime类来执行系统命令获取客户端的MAC地址。具体步骤如下: 1. 使用Runtime.getRuntime().exec()方法执行命令行命令"arp -a"。 2. 读取命令执行的输出流并解析,找到与本机IP地址对应的MAC地址。 第二种方法是通过网络接口获取。可以使用Java的NetworkInterface类来获取网络接口的信息。具体步骤如下: 1. 使用NetworkInterface.getNetworkInterfaces()方法获取所有网络接口的列表。 2. 遍历网络接口列表,找到符合条件的接口,如有IP地址且非回环接口。 3. 使用NetworkInterface.getHardwareAddress()方法获取MAC地址的字节数组。 4. 将字节数组转换为十六进制字符串表示。 需要注意的是,这两种方法都需要在具有相应权限的操作系统上运行。在某些情况下,可能无法获取MAC地址或需要额外的配置或权限。 综上所述,通过ARP命令或网络接口获取MAC地址是在Java获取客户端MAC地址的常见方法。具体使用哪种方法取决于操作系统和环境的要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值