java編程實(shí)現(xiàn)獲取服務(wù)器IP地址及MAC地址的方法
本文實(shí)例講述了java編程實(shí)現(xiàn)獲取服務(wù)器IP地址及MAC地址的方法。分享給大家供大家參考,具體如下:
已測(cè)系統(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();
}
}
希望本文所述對(duì)大家Java程序設(shè)計(jì)有所幫助。
相關(guān)文章
SSH框架網(wǎng)上商城項(xiàng)目第3戰(zhàn)之使用EasyUI搭建后臺(tái)頁(yè)面框架
SSH框架網(wǎng)上商城項(xiàng)目第3戰(zhàn)之使用EasyUI搭建后臺(tái)頁(yè)面框架,討論兩種搭建方式:基于frameset和基于easyUI,感興趣的小伙伴們可以參考一下2016-05-05
解決在啟動(dòng)eclipse的tomcat進(jìn)行訪問(wèn)時(shí)出現(xiàn)404問(wèn)題的方法
這篇文章主要介紹了解決在啟動(dòng)eclipse的tomcat進(jìn)行訪問(wèn)時(shí)出現(xiàn)404問(wèn)題的方法,感興趣的小伙伴們可以參考一下2016-04-04
SpringBoot集成Elasticsearch過(guò)程實(shí)例
這篇文章主要介紹了SpringBoot集成Elasticsearch過(guò)程實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Java實(shí)現(xiàn)簡(jiǎn)易學(xué)籍管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)易學(xué)籍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
如何配置cursor進(jìn)行Java springboot項(xiàng)目開(kāi)發(fā)
本文介紹了如何在Cursor IDE中配置Java和Spring Boot項(xiàng)目開(kāi)發(fā)環(huán)境,首先,設(shè)置了系統(tǒng)用戶級(jí)別的JDK配置,以便在多個(gè)項(xiàng)目之間切換時(shí)不需要重新配置,然后,配置了Gradle環(huán)境變量,并安裝了必要的Java開(kāi)發(fā)插件,感興趣的朋友跟隨小編一起看看2025-02-02
Java與Node.js利用AES加密解密出相同結(jié)果的方法示例
這篇文章主要介紹了Java與Node.js利用AES加密解密出相同結(jié)果的方法,文中給出了詳細(xì)的示例代碼,相信對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,需要的朋友們下面來(lái)一起看看吧。2017-02-02
使用Spring注解@EventListener實(shí)現(xiàn)監(jiān)聽(tīng)原理
這篇文章主要介紹了使用Spring注解@EventListener實(shí)現(xiàn)監(jiān)聽(tīng)原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

