为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > JAVA获取MAC地址

JAVA获取MAC地址

2018-03-14 11页 doc 29KB 10阅读

用户头像

is_637320

暂无简介

举报
JAVA获取MAC地址JAVA获取MAC地址 nbtstat nmbloopup -A Java 根据IP获取客户端Mac地址 博客分类: Java JavaIEJSPBeanServlet 需要对用户的 ip 和 mac 地址进行验证,这里用到获取客户端ip和mac地址的两个方法,留存。 1.获取客户端ip地址( 这个必须从客户端传到后台): jsp页面下,很简单,request.getRemoteAddr() ; 因为系统的VIew层是用JSF来实现的,因此页面上没法直接获得类似request,在bean里做了个强制转换 J...
JAVA获取MAC地址
JAVA获取MAC地址 nbtstat nmbloopup -A Java 根据IP获取客户端Mac地址 博客分类: Java JavaIEJSPBeanServlet 需要对用户的 ip 和 mac 地址进行验证,这里用到获取客户端ip和mac地址的两个方法,留存。 1.获取客户端ip地址( 这个必须从客户端传到后台): jsp页面下,很简单,request.getRemoteAddr() ; 因为系统的VIew层是用JSF来实现的,因此页面上没法直接获得类似request,在bean里做了个强制转换 Java代码 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地址。方法如下: Java代码 public String getMACAddress(String ip){ String str = ""; String macAddress = ""; try { Process p = Runtime.getRuntime().exec("nbtstat -A " + ip); InputStreamReader ir = new InputStreamReader(p.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) { macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length()); break; } } } } catch (IOException e) { e.printStackTrace(System.out); } return macAddress; } 完整代码: Java代码 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地址是可以通过注册修改的,不建议以此来作为限制依据~ 分享到: Ext Ajax:如何调用Ext.Ajax.request方法和 ... | JS获取客户端网卡物理地址(MAC) 22:48评论 / 浏览 (3 / 2847)相关推荐 评论 3 楼 zhuchao_ko 2011-09-27 Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress); 改成 e 2 楼 yangkai 2010-04-14 Html代码 Getting MAC Address From Javascript(IE Only)
//====================================================// java 获取客户端ip mac地址 2008-04-02 16:59 最近做一个安全系统,需要对用户的 ip 和 mac 地址进行验证,这里用到获取客户端ip和 mac地址的两个方法,留存。 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 getMACAddress(String ip){ String str = ""; String macAddress = ""; try { Process p = Runtime.getRuntime().exec("nbtstat -A " + ip); InputStreamReader ir = new InputStreamReader(p.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) { macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length()); break; } } } } catch (IOException e) { e.printStackTrace(System.out); } return macAddress; } 补充: 关于获取IP地址的方式,最近在linux下有一个教训,如果单纯通过InetAddress来获取 IP地址,就会出现在不同的机器上IP地址不同的问。 InetAddress.getLocalHost().getAddress() 实际上是根据hostname来获取IP地址的。 linux系统在刚刚装完默认的hostname是localhost,所以通过上面代码获取到的本机ip 就是127.0.0.1, 相对应,比如我的hostname就是rjlin.atsig.com 返回的ip地址确是 atsig.com的地址。暂时采用下面代码来处理,当然还不够灵活: public static byte[] getIp() throws UnknownHostException { byte[] b = InetAddress.getLocalHost().getAddress(); Enumeration allNetInterfaces = null; try { allNetInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { e.printStackTrace(); } InetAddress ip = null; NetworkInterface netInterface = null; while (allNetInterfaces.hasMoreElements()) { netInterface = (NetworkInterface) allNetInterfaces.nextElement(); if (netInterface.getName().trim().equals("eth0")){ Enumeration addresses = netInterface.getInetAddresses(); while (addresses.hasMoreElements()) { ip = (InetAddress) addresses.nextElement(); } break; } } if (ip != null && ip instanceof Inet4Address) { return b = ip.getAddress(); } return b; }
/
本文档为【JAVA获取MAC地址】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索