今天看啥
热点:

JAVA获取客户端MAC,web获取客户端MAC,Applet获取客户端MAC;JAVAweb开发;服务器端很难获取到客户端的MAC受路由器等....获取的到不正确或者被过滤掉;小弟,无奈写了一个客户端的Applet来获取客户端MAC然后再在传给服务器端;JS方法虽然也可行,但是手浏览器限制,还其他因素,不知道高手有何建议或者例子,可以建议;但目前这个可以实现服务器端获取客户端的MAC;经过测试使用的;

 

package com.alpha.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class GetMac {

 /**
  * java获取客户端网卡的MAC地址
  *
  * @param args
  */
 public static void main(String[] args) {
  GetMac get = new GetMac();
  System.out.println("1="+get.getMAC());
  System.out.println("2="+get.getMAC("127.0.0.1"));
 }

 // 1.获取客户端ip地址( 这个必须从客户端传到后台):
 // jsp页面下,很简单,request.getRemoteAddr() ;
 // 因为系统的VIew层是用JSF来实现的,因此页面上没法直接获得类似request,在bean里做了个强制转换

 // public String getMyIP() {
 // try {
 // FacesContext fc = FacesContext.getCurrentInstance();
 // HttpServletRequest request = (HttpServletRequest) fc
 // .getExternalContext().getRequest();
 // return request.getRemoteAddr();
 // } catch (Exception e) {
 // e.printStackTrace();
 // }
 // return "";
 // }

 // 2.获取客户端mac地址
 // 调用window的命令,在后台Bean里实现 通过ip来获取mac地址。方法如下:

 // 运行速度【快】
 public String getMAC() {
  String mac = null;
  try {
   Process pro = Runtime.getRuntime().exec("cmd.exe /c ipconfig/all");

   InputStream is = pro.getInputStream();
   BufferedReader br = new BufferedReader(new InputStreamReader(is));
   String message = br.readLine();

   int index = -1;
   while (message != null) {
    if ((index = message.indexOf("Physical Address")) > 0) {
     mac = message.substring(index + 36).trim();
     break;
    }
    message = br.readLine();
   }
   System.out.println(mac);
   br.close();
   pro.destroy();
  } catch (IOException e) {
   System.out.println("Can't get mac address!");
   return null;
  }
  return mac;
 }

 // 运行速度【慢】
 public String getMAC(String ip) {
  String str = null;
  String macAddress = null;
  try {
   Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
   InputStreamReader ir = new InputStreamReader(p.getInputStream());
   LineNumberReader input = new LineNumberReader(ir);
   for (; true;) {
    str = input.readLine();
    if (str != null) {
     if (str.indexOf("MAC Address") > 1) {
      macAddress = str
        .substring(str.indexOf("MAC Address") + 14);
      break;
     }
    }
   }
  } catch (IOException e) {
   e.printStackTrace(System.out);
   return null;
  }
  return macAddress;
 }
}

代码二

import java.net.NetworkInterface;

import java.net.SocketException;

import java.util.Enumeration;

 

/**

 *

 * @author MrWang

 * 获取客户端MAC地址

 *

 */

 

public class MacAddress {

    public static String hexByte(byte b) {

        String s = "000000" + Integer.toHexString(b);

        return s.substring(s.length() - 2);

    }

    public static String getMAC() {

        Enumeration el;

        String mac_s = "";

        try {

            el = NetworkInterface.getNetworkInterfaces();

            while (el.hasMoreElements()) {

                byte[] mac = el.nextElement().getHardwareAddress();

                if (mac == null)

                    continue;

                mac_s = hexByte(mac[0]) + "-" + hexByte(mac[1]) + "-"

                        + hexByte(mac[2]) + "-" + hexByte(mac[3]) + "-"

                        + hexByte(mac[4]) + "-" + hexByte(mac[5]);

                System.out.println( "MAC地址:"+mac_s);

 

            }

        } catch (SocketException e1) {

            e1.printStackTrace();

        }

        return mac_s;

    }

     

    public static void main(String[] args) {

        MacAddress m = new MacAddress();

        m.getMAC();

         

    }

}

评论暂时关闭