【问题标题】:How can i get MAC address of client machine in jsp如何在jsp中获取客户端机器的MAC地址
【发布时间】:2011-05-09 08:35:17
【问题描述】:

我正在开发一个用于安全投票系统的应用程序。我需要获取特定机器的 MAC 地址以设置为“投票机”。如何获取客户端机器的 MAC 地址?

【问题讨论】:

    标签: jsp jsf


    【解决方案1】:
                //InetAddress address = InetAddress.getLocalHost();
                InetAddress address = InetAddress.getByName("192.168.46.53");
    
                /*
                 * Get NetworkInterface for the current host and then read the
                 * hardware address.
                 */
                NetworkInterface ni = NetworkInterface.getByInetAddress(address);
                if (ni != null) {
                    byte[] mac = ni.getHardwareAddress();
                    if (mac != null) {
                        /*
                         * Extract each array of mac address and convert it to hexa with the
                         * following format 08-00-27-DC-4A-9E.
                         */
                        for (int i = 0; i < mac.length; i++) {
                            System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
                        }
                    } else {
                        // Address doesn't exist or is not accessible.
                    }
                } else {
                    // Network Interface for the specified address is not found.
                }
    

    【讨论】:

    • 如何使用 JSP 获取客户端的 MAC 地址?
    • 看这些视频教程,可能对你有帮助:) vtc.com/products/Java-2-JSP-and-Servlets-tutorials.htm
    • 是否抛出错误或异常?或者它返回了错误的结果?注意我们输入了一个虚拟 IP,你必须替换它
    • 不客气,请不要忘记将其推上并标记为答案:)
    • 我的 macAddress 为空
    【解决方案2】:

    以下是我实现的代码,它对我有用。此代码来自:Get mac address ip address in jsp java

    代码如下:

    <%@ page import="java.net.*" %>
    <%@ page import="java.util.*" %>
    <%@ page import="java.io.*" %>
    <%@ page import="java.util.*" %>
    
    
    <%
    InetAddress inetAddress;
    StringBuilder sb = new StringBuilder();
    String ipAddress="",macAddress="";
    int i=0;
    try {
        inetAddress=InetAddress.getLocalHost();
        ipAddress=inetAddress.getHostAddress();
        NetworkInterface network=NetworkInterface.getByInetAddress(inetAddress);
         byte[] hw=network.getHardwareAddress();
         for(i=0; i<hw.length; i++)
            sb.append(String.format("%02X%s", hw[i], (i < hw.length - 1) ? "-" : 
             ""));    
        macAddress=sb.toString();
      } catch(Exception e) {
       out.print("<br/>"+e.toString());
        macAddress="-";
      }
      out.print("<br/>"+ipAddress);
      out.print("<br/>"+macAddress);
     %>
    

    【讨论】:

      猜你喜欢
      • 2013-11-08
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 2011-01-02
      • 2014-10-04
      • 2017-10-28
      • 1970-01-01
      相关资源
      最近更新 更多