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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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", inetAddressTypeName(inetAddress)); //网络类型
				ipMacInfo.put("os", System.getProperty("os.name")); //系统名称
				ipMacInfo.put("mac", sMAC); //mac 地址
				ipMacInfo.put("cpu-arch", System.getProperty("os.arch")); //cpu架构
				ipMacInfo.put("network-arch", ni.getDisplayName()); //网卡名称
				return ipMacInfo;
			}

		} catch (SocketException e) {
			e.printStackTrace();
		} 
		return null;
	}

	private static String inetAddressTypeName(InetAddress inetAddress) {
		return (inetAddress instanceof Inet4Address) ? "ipv4" : "ipv6";
	}

	//通过第三方网站http://1111.ip138.com/ic.asp获取ip
    public static Map<String, String> getOpenNetworkIp() 
    {
		
		try {
			URLConnection openConnection = new URL("http://1111.ip138.com/ic.asp").open
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值