使用 Java 小程序获取网页上的 MAC 地址 [英] Getting MAC address on a web page using a Java applet

查看:28
本文介绍了使用 Java 小程序获取网页上的 MAC 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create an application where a web server can get the MAC Address of the clients logging in. The only possible way I could think of was to create a JAVA applet which contains java.net methods to find the mac address

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

I am using javascript to call the applet methods, but the browser is not allowing those methods to execute. Below is the applet I have created.

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 通常无法访问这些功能.为了避免这些限制,您需要一个签名小程序,以及一个策略文件.

Applets cannot normally access these functions for security reasons. To avoid these restrictions, you need a signed applet, along with a policy file.

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

You can then write a policy file which grants your applet access to the functionality it needs. If the user then grants your applet the necessary permissions (it will prompt for them), your applet can use the functions.

这篇关于使用 Java 小程序获取网页上的 MAC 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆