javascript - 使用 Java 小程序在网页上获取 MAC 地址

标签 javascript applet

我想创建一个应用程序,Web 服务器可以在其中获取登录客户端的 MAC 地址。我能想到的唯一可能的方法是创建一个包含 java.net 方法的 JAVA applet 来查找 mac 地址

我正在使用 javascript 调用小程序方法,但浏览器不允许执行这些方法。下面是我创建的小程序。

import java.applet.*;
import java.awt.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class AppletRunner extends Applet{
 // The method that will be automatically called  when the applet is started
    public void init()
    {
// It is required but does not need anything.
    }


//This method gets called when the applet is terminated
//That's when the user goes to another page or exits the browser.
    public void stop()
    {
    // no actions needed here now.
    }


//The standard method that you have to use to paint things on screen
//This overrides the empty Applet method, you can't called it "display" for example.

    public void paint(Graphics g)
    {
//method to draw text on screen
// String first, then x and y coordinate.
     g.drawString(getMacAddr(),20,20);
     g.drawString("Hello World",20,40);

    } 
  public String getMacAddr() {
   String macAddr= ""; 
    InetAddress addr;
 try {
  addr = InetAddress.getLocalHost();

        System.out.println(addr.getHostAddress());
        NetworkInterface dir = NetworkInterface.getByInetAddress(addr);
        byte[] dirMac = dir.getHardwareAddress();

        int count=0;
        for (int b:dirMac){
         if (b<0) b=256+b;
         if (b==0) {
               macAddr=macAddr.concat("00"); 
         }
         if (b>0){

          int a=b/16;
          if (a==10) macAddr=macAddr.concat("A");
          else if (a==11) macAddr=macAddr.concat("B");
          else if (a==12) macAddr=macAddr.concat("C");
          else if (a==13) macAddr=macAddr.concat("D");
          else if (a==14) macAddr=macAddr.concat("E");
          else if (a==15) macAddr=macAddr.concat("F");
          else macAddr=macAddr.concat(String.valueOf(a));
             a = (b%16);
          if (a==10) macAddr=macAddr.concat("A");
          else if (a==11) macAddr=macAddr.concat("B");
          else if (a==12) macAddr=macAddr.concat("C");
          else if (a==13) macAddr=macAddr.concat("D");
          else if (a==14) macAddr=macAddr.concat("E");
          else if (a==15) macAddr=macAddr.concat("F");
          else macAddr=macAddr.concat(String.valueOf(a));
         }
         if (count<dirMac.length-1)macAddr=macAddr.concat("-");
         count++;
        }

 } catch (UnknownHostException e) {
  // TODO Auto-generated catch block
  macAddr=e.getMessage();
 } catch (SocketException e) {
  // TODO Auto-generated catch block
  macAddr = e.getMessage();
 }
 return macAddr;
 }

  }

最佳答案

出于安全原因,Applet 通常无法访问这些功能。为避免这些限制,您需要一个 signed applet ,以及政策文件。

然后您可以编写一个策略文件,授予您的小程序访问所需功能的权限。如果用户随后授予您的小程序必要的权限(它会提示您提供这些权限),您的小程序就可以使用这些功能。

关于javascript - 使用 Java 小程序在网页上获取 MAC 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4467905/

相关文章:

javascript - 将 JSJWS 和 XMLHttpRequest.js 库导入 Node.js

java - 哪个 HTML 标记最适合用于 Java applet(APPLET、EMBED、OBJECT)?

java - 如何在java应用程序中显示卡纳达语文本字符串

JAR 文件中的 Java Applet

javascript - 第一次成功提交后提交停止工作

javascript - 以编程方式设置 Angular 4 复选框

javascript - 使用 jQuery 更改 HTML 字符串 : wont update the string

javascript - 合并嵌套、重叠的 <strong> 和 <em> 标签

java - Jpanel中的放大缩小功能

java - 除非调整窗口大小,否则新 Canvas 不可见