当前位置:编程学习 > JAVA >>

如何在JSp中获取MAC地址

如何在JSp中获取MAC地址 --------------------编程问答-------------------- http://lusile0680.blog.163.com/blog/static/433381902009128114919466/ --------------------编程问答--------------------

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

public class GetMACAddress { 
public String getMACAddress(String ipAddress) { 
String str = "", strMAC = "", macAddress = ""; 
try { 
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress); 
InputStreamReader ir = new InputStreamReader(pp.getInputStream()); 
LineNumberReader input = new LineNumberReader(ir); 
for (int i = 1; i < 100; i++) { 
str = input.readLine(); 
if (str != null) { 
if (str.indexOf("MAC Address") > 1) { 
strMAC = str.substring(str.indexOf("MAC Address") + 14, 
str.length()); 
break; 



} catch (IOException ex) { 
return "Can't Get MAC Address!"; 

// 
if (strMAC.length() < 17) { 
return "Error!"; 


macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5) 
+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11) 
+ ":" + strMAC.substring(12, 14) + ":" 
+ strMAC.substring(15, 17); 
// 
return macAddress; 


public static void main(String[] args) { 
GetMACAddress getMACAddress = new GetMACAddress(); 
System.out.println(getMACAddress.getMACAddress("59.78.63.38")); //获得该ip地址的mac地址 


public static String procAll(String str) { 
return procStringEnd(procFirstMac(procAddress(str))); 


public static String procAddress(String str) { 
int indexof = str.indexOf("Physical Address"); 
if (indexof > 0) { 
return str.substring(indexof, str.length()); 

return str; 


public static String procFirstMac(String str) { 
int indexof = str.indexOf(":"); 
if (indexof > 0) { 
return str.substring(indexof + 1, str.length()).trim(); 

return str; 


public static String procStringEnd(String str) { 
int indexof = str.indexOf("\r"); 
if (indexof > 0) { 
return str.substring(0, indexof).trim(); 

return str; 



只要写一个servlet来调用这个类的getMACAddress(String netip)方法就可以获得客户端的mac地址了,相信你会写jsp应该对servlet也不陌生吧,在jsp中调用servlet通过session传递返回的mac地址,加以判断就可以了 

mac地址是可以通过注册表修改的,不建议以此来作为限制依据~ --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 学习,,帮上上上楼顶 --------------------编程问答-------------------- --------------------编程问答-------------------- 用js实现 --------------------编程问答-------------------- 用Runtime.getRuntime().exec()
执行 ipconfig 命令 然后自己截取字符串吧
--------------------编程问答-------------------- 楼上说的这些都仅限于win32平台,而且还得是英文的CMD显示,
ipad,手机,部分中文显示的CMD,都是限制条件, --------------------编程问答-------------------- goooooooooood --------------------编程问答-------------------- 2楼的这个方法不错,长见识了,不过这如果是高并发的话,只怕系统就要给搞宕了。 --------------------编程问答-------------------- 这个要做到比较通用的话,得通过客户端本地程序来获取,如:ActiveX插件或JavaApplet。
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,