Java获取服务器IP、MAC地址

版权声明:拒绝伸手党!!!转载请注明出处: https://blog.csdn.net/m0_37574389/article/details/85013226
@Resource
    private WebServiceContext webServiceContext;

    public String getLoginUser() throws Exception {
        InetAddress ip;
        try {

            ip = InetAddress.getLocalHost();
            System.out.println("Current IP address : " + ip.getHostAddress());

            NetworkInterface network = NetworkInterface.getByInetAddress(ip);

            byte[] mac = network.getHardwareAddress();

            System.out.print("Current MAC address : ");

            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(sb.toString());
            return ip + ":" + sb.toString();

        } catch (UnknownHostException e) {

            e.printStackTrace();

        } catch (SocketException e) {

            e.printStackTrace();

        }
        return null;

    }

猜你喜欢

转载自blog.csdn.net/m0_37574389/article/details/85013226