Java gets the IP and MAC address of this computer
Some machines have a number of virtual network cards, access to the IP address when there are some surprises, so need some authentication:
Get MAC address public static String getmacaddress () {try {enumeration<networkinterface> allnetinterfaces
= Networkinterface.getnetworkinterfaces ();
Byte[] mac = null; while (Allnetinterfaces.hasmoreelements ()) {NetworkInterface netinterface = (networkinterface) ALLNETINTERFACES.N
Extelement ();
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 get Failed", e);
Return ""; }//Get IP address public static StringGetipaddress () {try {enumeration<networkinterface> allnetinterfaces = Networkinterface.getnetworkinterfa
CES ();
inetaddress IP = null; while (Allnetinterfaces.hasmoreelements ()) {NetworkInterface netinterface = (networkinterface) ALLNETINTERFACES.N
Extelement ();
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 ()
Can be very good to some non-physical network card or useless online filtering, and then take the online IPV4 address can be.
Here, there are some common:
1, get the current machine operating system
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 HTTP Access Proxy
/**
* Set HTTP Proxy */public
static void Sethttpproxy () {
Properties prop = System.getproperties ();
Set the address of the proxy server to use for HTTP access
prop.setproperty ("Http.proxyhost", http_proxy_host);
Sets the port
prop.setproperty ("Http.proxyport", Http_proxy_port) for HTTP access to the proxy server to use;
To set up a host that does not need to be accessed through a proxy server, you can use the * wildcard character and multiple addresses to separate
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.nonproxyhosts");
}
When the application is started, the set is done before accessing the HTTP request. Of course, http.nonproxyhosts can use no settings, which means that all HTTP requests go to proxy.
As for the 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 the Java acquisition of native IP and Mac instances, the need for friends can refer to, thank you for the support of this site!