Java 獲取本機的IP與MAC地址實現(xiàn)詳解
Java 獲取本機的IP與MAC地址
有些機器有許多虛擬的網(wǎng)卡,獲取IP地址時會出現(xiàn)一些意外,所以需要一些驗證:
// 獲取mac地址
public static String getMacAddress() {
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
byte[] mac = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
continue;
} else {
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) {
return sb.toString();
}
}
}
}
} catch (Exception e) {
_logger.error("MAC地址獲取失敗", e);
}
return "";
}
// 獲取ip地址
public static String getIpAddress() {
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
continue;
} else {
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = addresses.nextElement();
if (ip != null && ip instanceof Inet4Address) {
return ip.getHostAddress();
}
}
}
}
} catch (Exception e) {
_logger.error("IP地址獲取失敗", e);
}
return "";
}
以上的代碼中
netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()
能很好地把一些非物理網(wǎng)卡或無用網(wǎng)上過濾掉,然后再取網(wǎng)上的IPV4地址即可。
說到這里,還有一些常用的:
1、獲取當前機器的操作系統(tǒng)
public final static String WIN_OS = "WINDOWS";
public final static String MAC_OS = "MAC";
public final static String LINUX_OS = "LINUX";
public final static String OTHER_OS = "OTHER";
public static String getOS() {
if (SystemUtils.IS_OS_WINDOWS){
return WIN_OS;
}
if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX){
return MAC_OS;
}
if (SystemUtils.IS_OS_UNIX){
return LINUX_OS;
}
return OTHER_OS;
}
2、設(shè)置HTTP訪問代理
/**
* 設(shè)置http代理
*/
public static void setHttpProxy() {
Properties prop = System.getProperties();
// 設(shè)置http訪問要使用的代理服務(wù)器的地址
prop.setProperty("http.proxyHost", HTTP_PROXY_HOST);
// 設(shè)置http訪問要使用的代理服務(wù)器的端口
prop.setProperty("http.proxyPort", HTTP_PROXY_PORT);
// 設(shè)置不需要通過代理服務(wù)器訪問的主機,可以使用*通配符,多個地址用|分隔
prop.setProperty("http.nonProxyHosts", RemoteConfig.PROXT_FILTER_DOMAIN);
}
/**
* 移除http代理
*/
public static void removeHttpProxy() {
Properties prop = System.getProperties();
prop.remove("http.proxyHost");
prop.remove("http.proxyPort");
prop.remove("http.nonProxyHosts");
}
在應(yīng)用啟動時,訪問HTTP請求前,設(shè)置好就行。當然,http.nonProxyHosts可以不用設(shè)置,表示所有的HTTP請求都走代理。
至于HTTPS代理,類似可以這樣設(shè)置:
System.setProperty("https.proxyHost", "HTTP_PROXY_HOST");
System.setProperty("https.proxyPort", "HTTP_PROXY_PORT");
以上就是Java 獲取本機IP和 MAC的實例,有需要的朋友可以參考下,謝謝大家對本站的支持!
相關(guān)文章
java微信公眾號開發(fā)(搭建本地測試環(huán)境)
這篇文章主要介紹了java微信公眾號開發(fā),主要內(nèi)容有測試公眾號與本地測試環(huán)境搭建,需要的朋友可以參考下2015-12-12
Spring之關(guān)于PropertyDescriptor的擴展剖析
這篇文章主要介紹了Spring之關(guān)于PropertyDescriptor的擴展剖析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Spring-Cloud-Function-Spel?漏洞環(huán)境搭建
這篇文章主要介紹了Spring-Cloud-Function-Spel?漏洞復(fù)現(xiàn)及搭建方法,搭建方法也很簡單,首先需要安裝maven jdk,具體安裝過程跟隨小編一起看看吧2022-03-03

