java編程實現(xiàn)獲取服務(wù)器IP地址及MAC地址的方法
本文實例講述了java編程實現(xiàn)獲取服務(wù)器IP地址及MAC地址的方法。分享給大家供大家參考,具體如下:
已測系統(tǒng):
windows linux unix
排除127.0.0.1 和 0.0.0.0.1等非正常IP
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class IpUtil {
private IpUtil(){}
/**
* 此方法描述的是:獲得服務(wù)器的IP地址
* @author: zhangyang33@sinopharm.com
* @version: 2014年9月5日 下午4:57:15
*/
public static String getLocalIP() {
String sIP = "";
InetAddress ip = null;
try {
boolean bFindIP = false;
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
if (bFindIP) {
break;
}
NetworkInterface ni = (NetworkInterface) netInterfaces
.nextElement();
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (!ip.isLoopbackAddress()
&& ip.getHostAddress().matches(
"(\\d{1,3}\\.){3}\\d{1,3}")) {
bFindIP = true;
break;
}
}
}
} catch (Exception e) {
OutUtil.error(IpUtil.class, e.getMessage());
}
if (null != ip) {
sIP = ip.getHostAddress();
}
return sIP;
}
/**
* 此方法描述的是:獲得服務(wù)器的IP地址(多網(wǎng)卡)
* @author: zhangyang33@sinopharm.com
* @version: 2014年9月5日 下午4:57:15
*/
public static List<String> getLocalIPS() {
InetAddress ip = null;
List<String> ipList = new ArrayList<String>();
try {
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces
.nextElement();
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (!ip.isLoopbackAddress()
&& ip.getHostAddress().matches(
"(\\d{1,3}\\.){3}\\d{1,3}")) {
ipList.add(ip.getHostAddress());
}
}
}
} catch (Exception e) {
OutUtil.error(IpUtil.class, e.getMessage());
}
return ipList;
}
/**
* 此方法描述的是:獲得服務(wù)器的MAC地址
* @author: zhangyang33@sinopharm.com
* @version: 2014年9月5日 下午1:27:25
*/
public static String getMacId() {
String macId = "";
InetAddress ip = null;
NetworkInterface ni = null;
try {
boolean bFindIP = false;
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
if (bFindIP) {
break;
}
ni = (NetworkInterface) netInterfaces
.nextElement();
// ----------特定情況,可以考慮用ni.getName判斷
// 遍歷所有ip
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (!ip.isLoopbackAddress() // 非127.0.0.1
&& ip.getHostAddress().matches(
"(\\d{1,3}\\.){3}\\d{1,3}")) {
bFindIP = true;
break;
}
}
}
} catch (Exception e) {
OutUtil.error(IpUtil.class, e.getMessage());
}
if (null != ip) {
try {
macId = getMacFromBytes(ni.getHardwareAddress());
} catch (SocketException e) {
OutUtil.error(IpUtil.class, e.getMessage());
}
}
return macId;
}
/**
* 此方法描述的是:獲得服務(wù)器的MAC地址(多網(wǎng)卡)
* @author: zhangyang33@sinopharm.com
* @version: 2014年9月5日 下午1:27:25
*/
public static List<String> getMacIds() {
InetAddress ip = null;
NetworkInterface ni = null;
List<String> macList = new ArrayList<String>();
try {
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
ni = (NetworkInterface) netInterfaces
.nextElement();
// ----------特定情況,可以考慮用ni.getName判斷
// 遍歷所有ip
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (!ip.isLoopbackAddress() // 非127.0.0.1
&& ip.getHostAddress().matches(
"(\\d{1,3}\\.){3}\\d{1,3}")) {
macList.add(getMacFromBytes(ni.getHardwareAddress()));
}
}
}
} catch (Exception e) {
OutUtil.error(IpUtil.class, e.getMessage());
}
return macList;
}
private static String getMacFromBytes(byte[] bytes) {
StringBuffer mac = new StringBuffer();
byte currentByte;
boolean first = false;
for (byte b : bytes) {
if (first) {
mac.append("-");
}
currentByte = (byte) ((b & 240) >> 4);
mac.append(Integer.toHexString(currentByte));
currentByte = (byte) (b & 15);
mac.append(Integer.toHexString(currentByte));
first = true;
}
return mac.toString().toUpperCase();
}
}
希望本文所述對大家Java程序設(shè)計有所幫助。
相關(guān)文章
SSH框架網(wǎng)上商城項目第3戰(zhàn)之使用EasyUI搭建后臺頁面框架
SSH框架網(wǎng)上商城項目第3戰(zhàn)之使用EasyUI搭建后臺頁面框架,討論兩種搭建方式:基于frameset和基于easyUI,感興趣的小伙伴們可以參考一下2016-05-05
解決在啟動eclipse的tomcat進行訪問時出現(xiàn)404問題的方法
這篇文章主要介紹了解決在啟動eclipse的tomcat進行訪問時出現(xiàn)404問題的方法,感興趣的小伙伴們可以參考一下2016-04-04
Java實現(xiàn)簡易學(xué)籍管理系統(tǒng)
這篇文章主要為大家詳細介紹了Java實現(xiàn)簡易學(xué)籍管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
如何配置cursor進行Java springboot項目開發(fā)
本文介紹了如何在Cursor IDE中配置Java和Spring Boot項目開發(fā)環(huán)境,首先,設(shè)置了系統(tǒng)用戶級別的JDK配置,以便在多個項目之間切換時不需要重新配置,然后,配置了Gradle環(huán)境變量,并安裝了必要的Java開發(fā)插件,感興趣的朋友跟隨小編一起看看2025-02-02
Java與Node.js利用AES加密解密出相同結(jié)果的方法示例
這篇文章主要介紹了Java與Node.js利用AES加密解密出相同結(jié)果的方法,文中給出了詳細的示例代碼,相信對大家的學(xué)習(xí)或者工作能帶來一定的幫助,需要的朋友們下面來一起看看吧。2017-02-02
使用Spring注解@EventListener實現(xiàn)監(jiān)聽原理
這篇文章主要介紹了使用Spring注解@EventListener實現(xiàn)監(jiān)聽原理,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08

