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