Java如何获取本地mac地址和ip地址

Java获取本机的mac地址

 public String getLocalMac() {
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            //获取网卡,获取地址
            byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < mac.length; i++) {
                if (i != 0) {
                    sb.append("-");
                }
                //字节转换为整数
                int temp = mac[i] & 0xff;
                String str = Integer.toHexString(temp);
                if (str.length() == 1) {
                    sb.append("0" + str);
                } else {
                    sb.append(str);
                }
            }
            return sb.toString();
        } catch (Exception exception) {
        }
        return null;
    }

Java获取本地ip地址

 public String getLocalName() throws UnknownHostException {
        String ip="";
        try {
            InetAddress addr = InetAddress.getLocalHost();
            ip = addr.getHostAddress();
        }catch (Exception e){

        }
        return ip;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. Java根据MAC地址获取IP地址的方法: Java中可以使用InetAddress类来获取网络上的IP地址。但是,InetAddress类并没有提供直接的方法来获取MAC地址。在Java获取MAC地址需要通过操作系统提供的API来实现。下面是一种获取MAC地址的方法: ```java import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; public class MacUtil { public static String getMacAddress(String ipAddress) throws UnknownHostException, SocketException { InetAddress inetAddress = InetAddress.getByName(ipAddress); NetworkInterface network = NetworkInterface.getByInetAddress(inetAddress); 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) ? "-" : "")); } return sb.toString(); } } ``` 2. Java根据本地IP获取MAC地址的方法: Java中同样可以使用InetAddress类来获取本地IP地址获取本地MAC地址同样需要通过操作系统提供的API来实现。下面是一种获取本地MAC地址的方法: ```java import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; public class MacUtil { public static String getMacAddress() throws UnknownHostException, SocketException { InetAddress inetAddress = InetAddress.getLocalHost(); NetworkInterface network = NetworkInterface.getByInetAddress(inetAddress); 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) ? "-" : "")); } return sb.toString(); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

addresstool

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值