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