JAVA obtains the MAC address of the client, and java obtains the mac address.
The requirement today is that the background log should record the detailed access record information of the visitor, including the environment information.Browser information, IPI believe everyone canRequestBut thisMAC address of the clientI need to write a method to get it by myself. I have written a method and I will share it with you. The coding level of the younger brother is limited. What is the unreasonable design? Please do not try it.
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:luckyboyguo@126.com * @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:luckyboyguo@126.com * @author luckyboy * @QQ:263235040 */ public 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:luckyboyguo@126.com * @author luckyboy * @QQ:263235040 */ public static String filterMacAddress(final String ip, final String sourceString,final String macSeparator) { String result = ""; 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:luckyboyguo@126.com * @author luckyboy * @QQ:263235040 */ public 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:luckyboyguo@126.com * @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:luckyboyguo@126.com * @author luckyboy * @QQ:263235040 */ public static 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: