java获取机器ip、mac

import java.net.InetAddress;

import java.net.NetworkInterface;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Enumeration;

import java.util.List;

public class NetUtil {

public static String getIps(){

List<String> ips= new ArrayList<>();

try {

Enumeration<NetworkInterface> allNetInterfaces;

allNetInterfaces = NetworkInterface.getNetworkInterfaces();

while (allNetInterfaces.hasMoreElements()) {

NetworkInterface netInterface = allNetInterfaces.nextElement();

if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint()) {

//just print

Enumeration<InetAddress> inetAddresses = netInterface.getInetAddresses();

InetAddress iaddr;

while (inetAddresses.hasMoreElements()) {

iaddr = inetAddresses.nextElement();

if (iaddr != null) {

//ips.add(iaddr.getHostAddress());

System.out.println("aa:" + iaddr.getHostAddress());

}

}

continue;

}

Enumeration<InetAddress> inetAddresses = netInterface.getInetAddresses();

InetAddress iaddr;

while (inetAddresses.hasMoreElements()) {

iaddr = inetAddresses.nextElement();

if (iaddr != null) {

ips.add(iaddr.getHostAddress());

}

}

}

} catch (Exception ignored) {

}

Collections.sort(ips);

StringBuilder sb=new StringBuilder();

for(String mac:ips){

sb.append(mac);

}

return sb.toString();

}

public static String getMac(){

List<String> macs= new ArrayList<>();

try {

Enumeration<NetworkInterface> allNetInterfaces;

allNetInterfaces = NetworkInterface.getNetworkInterfaces();

byte[] mac;

while (allNetInterfaces.hasMoreElements()) {

NetworkInterface netInterface = allNetInterfaces.nextElement();

if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint()) {

mac = netInterface.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) ? "-" : ""));

}

if (sb.length() > 0) {

//macs.add(sb.toString());

System.out.println("aa:" + sb.toString());

}

}

continue;

}

mac = netInterface.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) ? "-" : ""));

}

if (sb.length() > 0) {

macs.add(sb.toString());

}

}

}

} catch (Exception ignored) {

}

Collections.sort(macs);

StringBuilder sb=new StringBuilder();

for(String mac:macs){

sb.append(mac);

}

return sb.toString();

}

public static void main(String[] args) {

String ret = getIps();

ret = getMac();

System.out.println(ret);

}

}

相关推荐
南山十一少3 小时前
Spring Security+JWT+Redis实现项目级前后端分离认证授权
java·spring·bootstrap
427724004 小时前
IDEA使用git不提示账号密码登录,而是输入token问题解决
java·git·intellij-idea
chengooooooo5 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦5 小时前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个5 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
南宫生7 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
计算机毕设定制辅导-无忧学长8 小时前
Maven 基础环境搭建与配置(一)
java·maven
风与沙的较量丶9 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
m0_748251729 小时前
SpringBoot3 升级介绍
java
极客先躯10 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?
java·数据库·mysql·数据库的锁·模式分·粒度分·属性分