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;&&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