有些機器有許多虛擬的網卡,獲取IP地址時會出現一些意外,因此須要一些驗證:html
1 // 獲取mac地址 2 public static String getMacAddress() { 3 try { 4 Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces(); 5 byte[] mac = null; 6 while (allNetInterfaces.hasMoreElements()) { 7 NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement(); 8 if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()) { 9 continue; 10 } else { 11 mac = netInterface.getHardwareAddress(); 12 if (mac != null) { 13 StringBuilder sb = new StringBuilder(); 14 for (int i = 0; i < mac.length; i++) { 15 sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); 16 } 17 if (sb.length() > 0) { 18 return sb.toString(); 19 } 20 } 21 } 22 } 23 } catch (Exception e) { 24 _logger.error("MAC地址獲取失敗", e); 25 } 26 return ""; 27 } 28 29 // 獲取ip地址 30 public static String getIpAddress() { 31 try { 32 Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces(); 33 InetAddress ip = null; 34 while (allNetInterfaces.hasMoreElements()) { 35 NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement(); 36 if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()) { 37 continue; 38 } else { 39 Enumeration<InetAddress> addresses = netInterface.getInetAddresses(); 40 while (addresses.hasMoreElements()) { 41 ip = addresses.nextElement(); 42 if (ip != null && ip instanceof Inet4Address) { 43 return ip.getHostAddress(); 44 } 45 } 46 } 47 } 48 } catch (Exception e) { 49 _logger.error("IP地址獲取失敗", e); 50 } 51 return ""; 52 }
以上的代碼中服務器
netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()
能很好地把一些非物理網卡或無用網上過濾掉,而後再取網上的IPV4地址便可。app
說到這裏,還有一些經常使用的:oop
一、獲取當前機器的操做系統ui
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; }
二、設置HTTP訪問代理spa
1 /** 2 * 設置http代理 3 */ 4 public static void setHttpProxy() { 5 Properties prop = System.getProperties(); 6 // 設置http訪問要使用的代理服務器的地址 7 prop.setProperty("http.proxyHost", HTTP_PROXY_HOST); 8 // 設置http訪問要使用的代理服務器的端口 9 prop.setProperty("http.proxyPort", HTTP_PROXY_PORT); 10 // 設置不須要經過代理服務器訪問的主機,能夠使用*通配符,多個地址用|分隔 11 prop.setProperty("http.nonProxyHosts", RemoteConfig.PROXT_FILTER_DOMAIN); 12 } 13 14 /** 15 * 移除http代理 16 */ 17 public static void removeHttpProxy() { 18 Properties prop = System.getProperties(); 19 prop.remove("http.proxyHost"); 20 prop.remove("http.proxyPort"); 21 prop.remove("http.nonProxyHosts"); 22 }
在應用啓動時,訪問HTTP請求前,設置好就行。固然,http.nonProxyHosts能夠不用設置,表示全部的HTTP請求都走代理。操作系統
至於HTTPS代理,相似能夠這樣設置:代理
System.setProperty("https.proxyHost", "HTTP_PROXY_HOST");
System.setProperty("https.proxyPort", "HTTP_PROXY_PORT");
轉載請註明原址:http://www.cnblogs.com/lekko/p/5888962.htmlcode