Java obtains the native IP and MAC address of the operating system

Source: Internet
Author: User

    1. Get the LAN IP and Mac (if the computer is not directly connected to the extranet), otherwise get the public IP

    2. Get public IP via third place

Package org.twt.zipjar.test;import java.io.bufferedreader;import java.io.ioexception;import  java.io.InputStream;import java.io.InputStreamReader;import java.net.Inet4Address;import  Java.net.inetaddress;import java.net.malformedurlexception;import java.net.networkinterface;import  java.net.SocketException;import java.net.URL;import java.net.URLConnection;import  Java.net.unknownhostexception;import java.nio.charset.charset;import java.util.enumeration;import  java.util.formatter;import java.util.hashmap;import java.util.locale;import java.util.map ;p ublic class networkutils {/** *  get a list of local IPs (for multiple network cards)  *  @return  */public  static map<string, object> getlocalinetmac ()  {Map<String, Object>  ipmacinfo = null;try {enumeration<networkinterface> networkinterfaces =  networkinterface.getnetwOrkinterfaces ();while  (Networkinterfaces.hasmoreelements ())  {networkinterface networkinterface  = networkinterfaces.nextelement (); Enumeration<inetaddress> inetaddresses = networkinterface.getinetaddresses ();while  ( Inetaddresses.hasmoreelements ())  {inetaddress inetaddress = inetaddresses.nextelement (); Ipmacinfo = pickinetaddress (Inetaddress, networkinterface);if  (ipMacInfo != null )  {return ipmacinfo;}}}}  catch  (socketexception e)  {e.printstacktrace ();} Return null;} Private static map<string, object> pickinetaddress (InetAddress inetAddress, Networkinterface ni)  {try {string name = ni.getdisplayname ();if  ( Name.contains ("Virtual ethernet adapter") | |  name.contains ("Virtual")  | |  name.contains ("VMnet"))  {return null;} if  (ni.isvirtual ()  | |  !ni.isup ()  | |  !ni.supportsmulticast ())  {return null;} Inetaddress localhost = inetaddress.getlocalhost ();if  (Inetaddress.issitelocaladdress () && localhost.gethostname (). Equals (Inetaddress.gethostname ()))  {formatter formatter  = new formatter (); String smac = null;byte[] macbuf = ni.gethardwareaddress ();for  (int i  = 0; i < macbuf.length; i++)  {smac = formatter.format ( Locale.getdefault (),  "%02x%s",macbuf[i],  (i < macbuf.length - 1)  ?  "-"  :  ""). toString ();} Formatter.close (); Map<string, object> ipmacinfo = new hashmap<string, object> (); Ipmacinfo.put ("hostname",  inetaddress.gethostname ());  //system Current Hostnameipmacinfo.put ("IP",  Inetaddress.gethostaddress ()); &NBSP;//IP address Ipmacinfo.put ("Ipnet",  inetaddresstypename (inetAddress));  //Network Type ipmacinfo.put ("OS",  system.getproperty ("Os.name"));  //system name Ipmacinfo.put ("Mac",  smac);  //mac  address Ipmacinfo.put ("Cpu-arch",  system.getproperty ("Os.arch"));  //cpu Architecture Ipmacinfo.put (" Network-arch ",  ni.getdisplayname ());  //nic name Return ipmacinfo;}}  catch  (socketexception e)  {e.printstacktrace ();}  catch  (unknownhostexception e)  {e.printstacktrace ();} Return null;} Private static string inetaddresstypename (inetaddress inetaddress)  {return  ( inetaddress instanceof inet4address)  ?  "IPv4"  :  "IPv6";} Get ipprivate static map<string, string>  through a third-party website http://1111.ip138.com/ic.asp Getopennetworkip ()  {try {urlconnection openconnection = new url ("http://1111. Ip138.com/ic.asp "). OpenConnection (); Openconnection.setdoinput (true); Openconnection.connect ();InputStream  is =   (InputStream) &NBSp;openconnection.getcontent (); Bufferedreader br = new bufferedreader (New inputstreamreader (Is,Charset.forName ("GBK" ))); Stringbuilder sb = new stringbuilder (); String str = null;while ((Str=br.readline ())!=null) {sb.append (str);} String htmlsrc = sb.tostring (). toLowerCase (Locale.getdefault ()); string starttag =  "<center>"; string endtag =  "</center>"; Htmlsrc = htmlsrc.substring (Htmlsrc.indexof ( Starttag) +starttag.length (),  htmlsrc.lastindexof (Endtag)); String openip = htmlsrc.substring (Htmlsrc.indexof ("[") +1, htmlsrc.lastindexof ("]"); String provider = htmlsrc.substring (Htmlsrc.lastindexof (":") +1); Map<string, string> resultmap = new hashmap<string, string> (); Resultmap.put ("Openip",  openip); Resultmap.put ("Provider",  provider); Br.close (); Return resultmap ;}  catch  (Malformedurlexception e)  {e.printstacktrace ();}  catch  (ioexception e)  {e.printstacktrace ();} Return null;} Public static void main (String[] args)  {Map<String, Object>  Localinetmac = getlocalinetmac (); System.out.println (LOCALINETMAC); Map<string, string> opennetworkip = getopennetworkip (); System.out.println (Opennetworkip);}}

If the scheme is successful in the Android,windows,linux, there may be some areas considered less thoughtful, but should cover the 90-95% pc and Android system, yes, it does not seem to support Android 2.2, but Android itself can get Mac

Public String getmacaddress () {Wifimanager Wifimanager = (wifimanager) context.getsystemservice (Context.wifi_service) ; String mac_address = (Wifimanager = = null?) ": Wifimanager.getconnectioninfo (). getmacaddress ()); mac_address = (mac_address = = null?" ": mac_address); return mac_a    ddress; }

Here are the effects that run on Android

Java obtains the native IP and MAC address of the operating system

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.