Get the user's MAC address based on the IP address

Source: Internet
Author: User

You can use the following code to get the MAC address, depending on your actual needs.

/** *  get MAC address  *  @param  ipAddress 127.0.0.1 *  @return based on IP address  *   @throws  SocketException *  @throws  unknownhostexception */public static  string getlocalmac (string ipaddress)  throws socketexception,unknownhostexception  {// TODO Auto-generated method stubString str =  ""; string macaddress =  "";final string loopback_address =  "127.0.0.1";//  If 127.0.0.1, the local MAC address is obtained. if  (Loopback_address.equals (ipAddress))  {InetAddress inetAddress =  Inetaddress.getlocalhost ();//  It seems that this method requires JDK1.6. Byte[] mac = networkinterface.getbyinetaddress (inetaddress). Gethardwareaddress ();//  The following code is to assemble the MAC address into Stringstringbuilder sb = new stringbuilder ();for  (int i =  0; i < mac.length; i++)  {if  (i != 0)  {sb.append ("-");}  mac[i] & 0xFF  is to convert byte to a positive integer string s = integer.tohexstring (Mac[i] &NBSP;&AMP;&NBSP;0XFF); Sb.append (S.length ()  == 1 ? 0 + s : s);}   Change all lowercase letters of a string to uppercase to become a regular MAC address and return macaddress = sb.tostring (). Trim (). toUpperCase ();return  macAddress;}  else {//  get the MAC address of the non-local IP try {system.out.println (ipAddress); Process p = runtime.getruntime (). EXEC ("nbtstat -a "  + ipaddress); System.out.println ("===process==" +p); Inputstreamreader ir = new inputstreamreader ( P.getinputstream ()); Bufferedreader br = new bufferedreader (IR);while  ((Str = br.readline ())  != null)  {if (Str.indexof ("Mac") >1) {macaddress = str.substring ("Mac") + 9, str.length ()); Macaddress = macaddress.trim (); System.out.println ("macAddress:"  + macaddress); P.destroy (); Br.close (); ir.cLose ();}  catch  (Ioexception ex)  {}return macaddress;}} /** *  return IP address via httpservletrequest  *  *  @param  request *             HttpServletRequest *  @return  ip  String *  @throws  exception */public static string getipaddr ( Httpservletrequest request)  throws exception {string ip = request.getheader ("X-forwarded-for");if  (ip == null | |  ip.length ()  == 0 | |   "Unknown". Equalsignorecase (IP))  {ip = request.getheader ("Proxy-client-ip");} if  (ip == null | |  ip.length ()  == 0 | |   "Unknown". Equalsignorecase (IP))  {ip = request.getheader ("Wl-proxy-client-ip");} if  (ip == null | |  ip.length ()  == 0 | |   "Unknown". Equalsignorecase (IP))  {ip&nbsP;= request.getheader ("Http_client_ip");} if  (ip == null | |  ip.length ()  == 0 | |   "Unknown". Equalsignorecase (IP))  {ip = request.getheader ("Http_x_forwarded_for");} if  (ip == null | |  ip.length ()  == 0 | |   "Unknown". Equalsignorecase (IP))  {ip = request.getremoteaddr ();} Return ip;}

Get the MAC address of the user based on the IP 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.