java如何从http请求中获取mac(支持PC、手机)

在网上找了很多资料,大部分都比较坑,不是通过dos命令就是用nbtstat -A的方式,后来找到一个有残缺的,大概只能通过web获取
package com.wt.jwzh.common.utils;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**

  • @date 2018年5月22日 下午12:09:13 * */

public class UdpGetClientMacAddr {
private String sRemoteAddr;

private int iRemotePort=137;

private byte[] buffer = new byte[1024];

private DatagramSocket ds=null;

public UdpGetClientMacAddr(String strAddr) throws Exception{  
    sRemoteAddr = strAddr;  
    ds = new DatagramSocket();  
}  

public final DatagramPacket send(final byte[] bytes) throws IOException {  
    DatagramPacket dp = new DatagramPacket(bytes,bytes.length,InetAddress.getByName(sRemoteAddr),iRemotePort);  
    ds.send(dp);  
    return dp;  
}  

public final DatagramPacket receive() throws Exception {  
    DatagramPacket dp = new DatagramPacket(buffer,buffer.length);  
    ds.receive(dp);  
    return dp;  
}  
public byte[] GetQueryCmd() throws Exception {  
    byte[] t_ns = new byte[50];  
    t_ns[0] = 0x00;  
    t_ns[1] = 0x00;  
    t_ns[2] = 0x00;  
    t_ns[3] = 0x10;  
    t_ns[4] = 0x00;  
    t_ns[5] = 0x01;  
    t_ns[6] = 0x00;  
    t_ns[7] = 0x00;  
    t_ns[8] = 0x00;  
    t_ns[9] = 0x00;  
    t_ns[10] = 0x00;  
    t_ns[11] = 0x00;  
    t_ns[12] = 0x20;  
    t_ns[13] = 0x43;  
    t_ns[14] = 0x4B;  

    for(int i = 15; i < 45; i++){  
        t_ns[i] = 0x41;  
    }  
    t_ns[45] = 0x00;  
    t_ns[46] = 0x00;  
    t_ns[47] = 0x21;  
    t_ns[48] = 0x00;  
    t_ns[49] = 0x01;  
    return t_ns;  
}  
public final String GetMacAddr(byte[] brevdata) throws Exception {  
    // 获取计算机名  
    int i = brevdata[56] * 18 + 56;  
    String sAddr="";  
    StringBuffer sb = new StringBuffer(17);  
    // 先从第56字节位置,读出Number Of Names(NetBIOS名字的个数,其中每个NetBIOS Names Info部分占18个字节)  
    // 然后可计算出“Unit ID”字段的位置=56+Number Of Names×18,最后从该位置起连续读取6个字节,就是目的主机的MAC地址。  
    for(int j = 1; j < 7;j++)  
    {  
        sAddr = Integer.toHexString(0xFF & brevdata[i+j]);  
        if(sAddr.length() < 2)  
        {  
            sb.append(0);  
        }  
        sb.append(sAddr.toUpperCase());  
        if(j < 6) sb.append(':');  
    }  
    return sb.toString();  
}  

public final void close() throws Exception {  
    ds.close();  
}  

public final String GetRemoteMacAddr() throws Exception {  
    byte[] bqcmd = GetQueryCmd();  
    send(bqcmd);  
    DatagramPacket dp = receive();  
    String smac = GetMacAddr(dp.getData());  
    close();  
    return smac;  
}  

}
嗯,求正确的获取方式

你这个是获取同一个局域网的mac,公网上是获取不到终端的mac的,除非通过web 插件本地获取再上传

mac地址只有在局域网里才能获取到,过第三层交换机就获取不到了

首先要说的是:本方法可以支持外网机器的mac地址获取。

 import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
* 获取MAC地址
* @author
* 2011-12
*/
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; 
   }

   /** 
   * 
   * @param cmd 第一个命令 
   * @param another 第二个命令 
   * @return  第二个命令的执行结果 
   */
   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; 
   }



   /** 
   * 
   * @param ip 目标ip,一般在局域网内 
   * @param sourceString 命令处理的结果字符串 
   * @param macSeparator mac分隔符号 
   * @return mac地址,用上面的分隔符号表示 
   */
   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; //如果有多个IP,只匹配本IP对应的Mac. 
       } 
     }

     return result; 
   }



   /** 
   * 
   * @param ip 目标ip 
   * @return  Mac Address 
   * 
   */
   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; 
   } 

   /** 
   * @param ip 目标ip 
   * @return  Mac Address 
   * 
   */
   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; 
   } 

   /**
   * 获取MAC地址 
   * @return 返回MAC地址
   */
   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("220.181.111.148"));
   }

}

https://blog.csdn.net/a419419/article/details/78752417,建议使用页面实现,判断完成后,ajax提交给java

这个方法应该也只能获取到内网机器通信的mac地址,如果你web在公网,获取的mac不会是客户端的

proc

img