Today, there is a need, is the background of the log to record the visitor's detailed access record information, including the environment information, as for the browser information, IP of these can be obtained from the request , but this client's MAC address need to write a method to get, I wrote a method, now take out and share with you, younger brother coding level is limited, what design unreasonable place, hope you big God do not spray.
The code is as follows:
Package Com.app.archive.util;import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.util.regex.matcher;import java.util.regex.pattern;/** * @e-mail:[email protected] * @author Luckyboy * @QQ : 263235040 */public class Getmacaddress {public static string Callcmd (string[] cmd) {string result = ""; String line = ""; try {Process proc = runtime.getruntime (). exec (CMD); InputStreamReader is = new InputStreamReader (Proc.getinputstream ()); BufferedReader br = new BufferedReader (IS); while (line = Br.readline ()) = null) {result + = line; }} catch (Exception e) {e.printstacktrace (); } return result; }/** * @e-mail:[email protected] * @author luckyboy * @QQ: 263235040 */Publ IC Static String callcmd (string[] cmd,string[] another) { String result = ""; String line = ""; try {Runtime RT = Runtime.getruntime (); Process proc = rt.exec (cmd); Proc.waitfor (); proc = Rt.exec (another); InputStreamReader is = new InputStreamReader (Proc.getinputstream ()); BufferedReader br = new BufferedReader (IS); while (line = Br.readline ()) = null) {result + = line; }} catch (Exception e) {e.printstacktrace (); } return result; }/** * @e-mail:[email protected] * @author luckyboy * @QQ: 263235040 */Public static string filtermacaddress (final string IP, final string sourcestring,final string macseparator) {string re Sult = ""; String RegExp = "((([0-9,a-f,a-f]{1,2} + Macseparator +") {1,5}) [0-9,a-f,a-f]{1,2}]; Pattern pattern = pattern.compile (REGEXP); Matcher Matcher = Pattern.matcher (sourcestring); while (Matcher.find ()) {result = Matcher.group (1); if (Sourcestring.indexof (IP) <= sourcestring.lastindexof (Matcher.group (1))) {break; }} return result; }/** * @e-mail:[email protected] * @author luckyboy * @QQ: 263235040 */P Ublic static string getmacinwindows (final string IP) {string result = ""; string[] cmd = {"cmd", "/C", "Ping" + IP}; String[] another = {"cmd", "/C", "Arp-a" }; String Cmdresult = Callcmd (Cmd,another); result = Filtermacaddress (Ip,cmdresult, "-"); return result; }/** * @e-mail:[email protected] * @author Luckyboy *@QQ: 263235040 */public static string Getmacinlinux (final string IP) {string result = ""; string[] cmd = {"/bin/sh", "-C", "ping" + IP + "-C 2 && Arp-a "}; String Cmdresult = callcmd (cmd); result = Filtermacaddress (Ip,cmdresult, ":"); return result; }/** * @e-mail:[email protected] * @author luckyboy * @QQ: 263235040 */Public STA Tic string getmacaddress (String IP) {string macAddress = ""; macAddress = Getmacinwindows (IP). Trim (); if (macaddress==null| | "". Equals (macAddress)) {macAddress = Getmacinlinux (IP). Trim (); } return macAddress; } public static void Main (string[] args) {System.out.println (getmacaddress ("127.0.0.1")); } }
Test results:
Java gets the MAC address of the client