jsp中如何获取客户端机器的MAC地址

发布于 2024-11-05 21:05:46 字数 71 浏览 0 评论 0原文

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

I am developing one application for secure voting system. I need to get MAC address of a particular machine to set as 'voting machine'. How can I get MAC address of a client machine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大姐,你呐 2024-11-12 21:05:46
            //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.
            }
            //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.
            }
Saygoodbye 2024-11-12 21:05:46

以下是我实现的代码,它对我有用。此代码来自: 获取mac地址 jsp java中的ip地址

代码如下:

<%@ 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);
 %>

The following is the code i implemented and it worked for me. This code is from : Get mac address ip address in jsp java.

The Code is as follows:

<%@ 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);
 %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文