JAVA擷取用戶端的MAC地址,java擷取mac

來源:互聯網
上載者:User

JAVA擷取用戶端的MAC地址,java擷取mac

今天有個需求,就是背景日誌要記錄下訪問者的詳細訪問記錄資訊,包括環境資訊,至於瀏覽器資訊、IP啦這些的相信大家都可以從request中拿到,但是這個用戶端的MAC地址需要自己寫個方法擷取,我寫了一個方法,現拿出來和大家分享,小弟編碼水平有限,有什麼設計不合理的地方,望各位大神勿噴。

  代碼如下:

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"));     }    }

測試結果:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.