爱问知识人 爱问共享资料 医院库

JAVA如何根据IP获取客户端Mac地址?

首页

JAVA如何根据IP获取客户端Mac地址?

JSF/JAVA如何根据IP获取客户端Mac地址?

提交回答

全部答案

    2018-03-27 06:08:34
  •   需要对用户的 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 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 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; }。
      

    李***

    2018-03-27 06:08:34

类似问题

换一换
  • 系统软件 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新资料 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈
关注
爱问

关注爱问微信公众号,开启知识之旅,随时随地了解最新资讯。

确定举报此问题

举报原因(必选):