Java to get the IP and MAC address of the machine
Some machines have many virtual network cards, and some accidents will occur when obtaining IP addresses, so some verification is needed:
// Get the mac address 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 address acquisition failed", e); } return ""; } // Get the ip address 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 address acquisition failed", e); } return ""; } In the above code
netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()
It can filter out some non-physical network cards or useless networks well, and then retrieve the IPV4 address on the Internet.
Speaking of this, there are some commonly used ones:
1. Obtain the operating system of the current machine
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. Set up HTTP access proxy
/** * Set http proxy*/ public static void setHttpProxy() { Properties prop = System.getProperties(); // Set http to access the address of the proxy server to be used prop.setProperty("http.proxyHost", HTTP_PROXY_HOST); // Set http to access the port of the proxy server to be used prop.setProperty("http.proxyPort", HTTP_PROXY_PORT); // Setting up a host that does not need to be accessed through a proxy server can use the * wildcard character, and multiple addresses are separated by | with prop.setProperty("http.nonProxyHosts", RemoteConfig.PROXT_FILTER_DOMAIN); } /** * Remove http proxy*/ public static void removeHttpProxy() { Properties prop = System.getProperties(); prop.remove("http.proxyHost"); prop.remove("http.proxyPort"); prop.remove("http.nonProxyHost"); prop.remove("http.nonProxyHost"); } When the application starts, just set it before accessing the HTTP request. Of course, http.nonProxyHosts can be set without setting, which means that all HTTP requests are proxyed.
As for HTTPS proxy, you can set it like this:
System.setProperty("https.proxyHost", "HTTP_PROXY_HOST");
System.setProperty("https.proxyPort", "HTTP_PROXY_PORT");
The above is an example of Java obtaining native IP and MAC. Friends in need can refer to it. Thank you for your support for this site!