Java获取IP和MAC地址

参考修改:https://blog.csdn.net/yztezhl/article/details/50058263  

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ComputerUtil {

	public static String getMACAddress(){
		String osName = getosName().toLowerCase();

		Process p = null;
		try{
			String separator = "";
			if(osName.startsWith("windows")){
				p = Runtime.getRuntime().exec("ipconfig /all");
				separator = "-";// 不同系统mac地址的链接符号不一样
			} else if(osName.startsWith("linux")){
				p = Runtime.getRuntime().exec("/sbin/ifconfig -a");
				separator = ":";
			} else {
				throw new UnsupportedOperationException("不支持的操作系统");
			}
			
			BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line = "";
			String result = "";
			while ((line = br.readLine()) != null) {
				result += line;
			}
			br.close();

			return filterMacAddress(getIpAdress(), result, separator);
		} catch (IOException e){
			e.printStackTrace();
		}
		return "";

	}
	
	public static String filterMacAddress(String ip, String processResult, String separator){
		String result = "";  
        String regExp = "((([0-9,A-F,a-f]{1,2}" + separator + "){1,5})[0-9,A-F,a-f]{1,2})";  
        Pattern pattern = Pattern.compile(regExp);  
        Matcher matcher = pattern.matcher(processResult);  
        while(matcher.find()){  
            result = matcher.group(1);  
            if((-1 <= processResult.indexOf(ip)) && (-1 <= processResult.lastIndexOf(result))) {
            	return result;// 如果有多个IP,只匹配本IP对应的Mac.
            }  
        }
  
        return result;  
	}

	@SuppressWarnings("rawtypes")
	public static String getIpAdress(){
		String sIP = "";
		InetAddress ip = null; 
		try {
			//如果是Windows操作系统
			if(isWindowsOS()){
				ip = InetAddress.getLocalHost();
                return ip.getHostAddress();
			} else{// 如果是Linux操作系统
				//根据网卡取本机配置的IP  
				Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
				while (allNetInterfaces.hasMoreElements()) {
					NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();

					Enumeration addresses = netInterface.getInetAddresses();
					while (addresses.hasMoreElements()) {
						ip = (InetAddress) addresses.nextElement();
						if (ip != null && ip instanceof Inet4Address) {
							if(!ip.getHostAddress().startsWith("127.0.0.1")){
								return ip.getHostAddress();
							}
						} 
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return sIP;
	}

	public static String getosName() {
		return System.getProperty("os.name");
	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值