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);

}

}

相关推荐
JAVA学习通6 分钟前
《大营销平台系统设计实现》 - 营销服务 第5节:抽奖前置规则过滤
java·数据库·github
斯特凡今天也很帅8 分钟前
新建数据源报错No bean named ‘SqlSessionFactorykf‘ available
java·数据库·spring boot·mybatis
带刺的坐椅9 分钟前
用 Solon AI 从零构建 MCP 工具服务:让 AI Agent 拥有真实世界的能力
java·ai·solon·mcp·solon-ai
TheRouter14 分钟前
PromptCaching 工程实践:把LLM 调用成本砍掉80%
java·后端·spring·ai
寻道码路14 分钟前
LangChain4j Java AI 应用开发实战(二):大模型参数调优实战:Temperature、TopP、MaxTokens 深度解析
java·开发语言·人工智能·aigc
SimonKing29 分钟前
IP定位库的完美替代品:ip2region,开源、免费!
java·后端·程序员
XiYang-DING30 分钟前
【Spring】Lombok
java·后端·spring
凤山老林31 分钟前
AI辅助编程:Copilot在Java开发中的最佳实践
java·人工智能·copilot
ew4521831 分钟前
【Java】Apache POI 终极封装:支持多表格循环、图片插入、日期格式化的Word导出工具类(兼容POI3.17+)
java·word·apache
铁打的阿秀31 分钟前
IDEA启动项目报错: 加载主类 com.seeburger.webedi.system.SystemApplication 时出现 LinkageError
java·ide·intellij-idea