Java gets the IP and MAC address of the machine

Source: Internet
Author: User
Tags get ip

Some machines have many virtual network cards, and there are some surprises when acquiring an IP address, so some verification is required:

1     //Get MAC Address2      Public StaticString getmacaddress () {3         Try {4Enumeration<networkinterface> allnetinterfaces =networkinterface.getnetworkinterfaces ();5             byte[] Mac =NULL;6              while(Allnetinterfaces.hasmoreelements ()) {7NetworkInterface NetInterface =(NetworkInterface) allnetinterfaces.nextelement ();8                 if(Netinterface.isloopback () | | netinterface.isvirtual () | |!Netinterface.isup ()) {9                     Continue;Ten}Else { OneMac =netinterface.gethardwareaddress (); A                     if(Mac! =NULL) { -StringBuilder SB =NewStringBuilder (); -                          for(inti = 0; i < mac.length; i++) { theSb.append (String.Format ("%02x%s", Mac[i], (i < mac.length-1)? "-" : "")); -                         } -                         if(Sb.length () > 0) { -                             returnsb.tostring (); +                         } -                     } +                 } A             } at}Catch(Exception e) { -_logger.error ("MAC address acquisition failed", e); -         } -         return""; -     } -  in     //Get IP Address -      Public StaticString getipaddress () { to         Try { +Enumeration<networkinterface> allnetinterfaces =networkinterface.getnetworkinterfaces (); -inetaddress IP =NULL; the              while(Allnetinterfaces.hasmoreelements ()) { *NetworkInterface NetInterface =(NetworkInterface) allnetinterfaces.nextelement (); $                 if(Netinterface.isloopback () | | netinterface.isvirtual () | |!Netinterface.isup ()) {Panax Notoginseng                     Continue; -}Else { theEnumeration<inetaddress> addresses =netinterface.getinetaddresses (); +                      while(Addresses.hasmoreelements ()) { AIP =addresses.nextelement (); the                         if(IP! =NULL&& IPinstanceofinet4address) { +                             returnip.gethostaddress (); -                         } $                     } $                 } -             } -}Catch(Exception e) { the_logger.error ("IP address acquisition failed", e); -         }Wuyi         return""; the}

In the code above

Netinterface.isloopback () | | Netinterface.isvirtual () | | ! Netinterface.isup ()

Can be very good to some non-physical network card or useless online filter out, and then take the online IPV4 address.

Speaking of which, there are some commonly used:

1. Get the operating system of the current machine

     Public Final StaticString win_os = "WINDOWS";  Public Final StaticString mac_os = "MAC";  Public Final StaticString linux_os = "LINUX";  Public Final StaticString Other_os = "other";  Public StaticString Getos () {if(systemutils.is_os_windows) {returnWin_os; }        if(Systemutils.is_os_mac | |systemutils.is_os_mac_osx) {            returnMac_os; }        if(Systemutils.is_os_unix) {returnLinux_os; }        returnOther_os; }

2. Set HTTP Access Proxy

1     /**2 * Set HTTP proxy3      */4      Public Static voidSethttpproxy () {5Properties prop =system.getproperties ();6         //set HTTP access to the address of the proxy server to use7Prop.setproperty ("Http.proxyhost", http_proxy_host);8         //to set the port for HTTP access to the proxy server to use9Prop.setproperty ("Http.proxyport", http_proxy_port);Ten         //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 separated by | OneProp.setproperty ("Http.nonproxyhosts", remoteconfig.proxt_filter_domain); A     } -      -     /** the * Remove HTTP proxy -      */ -      Public Static voidRemovehttpproxy () { -Properties prop =system.getproperties (); +Prop.remove ("Http.proxyhost"); -Prop.remove ("Http.proxyport"); +Prop.remove ("Http.nonproxyhosts"); A}

When the app starts, set the line before accessing the HTTP request. Of course, http.nonproxyhosts can not be set, which means that all HTTP requests go through the proxy.

As for HTTPS proxies, this can be set up like this:

System.setproperty ("Https.proxyhost", "Http_proxy_host");
System.setproperty ("Https.proxyport", "Http_proxy_port");

Reprint Please specify original site: http://www.cnblogs.com/lekko/p/5888962.html

Java gets the IP and MAC address of the machine

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.