Get device information--Get client IP address and MAC address

Source: Internet
Author: User

1. Get the local IP (possibly intranet ip,192.168.xxx.xxx)
 /*** Get Local IP * *@return     */     Public StaticString getlocalipaddress () {Try{Enumeration<networkinterface> en =networkinterface. getnetworkinterfaces ();  while(En.hasmoreelements ()) {NetworkInterface ni=en.nextelement (); Enumeration<inetaddress> Enip =ni.getinetaddresses ();  while(Enip.hasmoreelements ()) {inetaddress inet=enip.nextelement (); if(!inet.isloopbackaddress ()&& (inetinstanceofinet4address)) {                        returninet.gethostaddress (). toString (); }                }            }        } Catch(SocketException e) {//TODO auto-generated Catch blockE.printstacktrace (); }         return"0"; }</inetaddress></networkinterface>
2. Obtaining a network IP is more accurate than the above method, but is executed asynchronously.
/*** Obtain the IP of the extranet (to access the URL, to be placed in the background line thread processing) * * @Title: Getnetip * @Description: *@param @return     * @returnString *@throws     */     Public StaticString Getnetip () {URL Infourl=NULL; InputStream instream=NULL; String Ipline= ""; HttpURLConnection httpconnection=NULL; Try{Infourl=NewURL ("https://ip168.com/"); URLConnection Connection=infourl.openconnection (); Httpconnection=(httpurlconnection) connection; intResponsecode =Httpconnection.getresponsecode (); if(Responsecode = =HTTPURLCONNECTION.HTTP_OK) {instream=Httpconnection.getinputstream (); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (instream, "Utf-8")); StringBuilder Strber=NewStringBuilder (); String Line=NULL;  while(line = Reader.readline ())! =NULL) Strber.append ( line+ "\ n"); Pattern Pattern=Pattern. Compile ("((?:(? : 25[0-5]|2[0-4]\\d| ((1\\d{2}) | ([1-9]?\\d))) \\.) {3} (?: 25[0-5]|2[0-4]\\d| ((1\\d{2}) | ([1-9]?\\d))]) "); Matcher Matcher=Pattern.matcher (strber.tostring ()); if(Matcher.find ()) {Ipline=Matcher.group (); }            }         } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally {            Try{instream.close ();            Httpconnection.disconnect (); } Catch(IOException e) {e.printstacktrace (); }        }        returnIpline; }
3. Get MAC Address
 Public StaticString getlocalmacaddress () {//without a cached address, the queryString mac_s = ""; Try {            byte[] mac; NetworkInterface NE=networkinterface.getbyinetaddress (Inetaddress.getbyname (Getlocalipaddress ())); Mac=ne.gethardwareaddress (); mac_s=Byte2hex (MAC); } Catch(Exception e) {}mac_s; returnmac_s; }

Get device information--Get client IP address and MAC address

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.