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

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

问题描述

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

我使用javascript调用applet的方法,但是浏览器未允许那些方法去执行。下面是我创建的小程序。

 进口java.applet中的*。
进口java.awt中的*。
进口java.net.InetAddress中;
进口java.net.NetworkInterface;
进口java.net.SocketException异常;
进口的java.net.UnknownHostException;公共类AppletRunner扩展的Applet {
 //将当小程序开始自动调用的方法
    公共无效的init()
    {
//这是必需的,但并不需要什么。
    }
//此方法被当小程序被终止叫
//当用户进入到另一个页面或退出浏览器。
    公共无效停止()
    {
    //现在需要在这里没有动作。
    }
//你有标准的方法,用油漆在屏幕上的东西
//这将覆盖空Applet的方法,你不能把它称为显示器为例。    公共无效漆(图形G)
    {
//方法在屏幕上绘制文本
//串首先,则x和y坐标。
     g.drawString(getMacAddr(),20,20);
     g.drawString(Hello World的,20,40);    }
  公共字符串getMacAddr(){
   字符串MACADDR =;
    InetAddress的地址;
 尝试{
  ADDR = InetAddress.getLocalHost();        的System.out.println(addr.getHostAddress());
        NetworkInterface的DIR = NetworkInterface.getByInetAddress(地址);
        字节[] = dirMac dir.getHardwareAddress();        诠释计数= 0;
        对于(INT A:dirMac){
         如果(二℃,)B = 256 + B;
         如果(二== 0){
               MACADDR = macAddr.concat(00);
         }
         如果(B&0){          INT A = B / 16;
          如果(A == 10)MACADDR = macAddr.concat(A);
          否则如果(一个== 11)MACADDR = macAddr.concat(B);
          否则如果(一个== 12)MACADDR = macAddr.concat(C);
          否则如果(一个== 13)MACADDR = macAddr.concat(D);
          否则如果(一个== 14)MACADDR = macAddr.concat(E);
          否则如果(一个== 15)MACADDR = macAddr.concat(F);
          别的MACADDR = macAddr.concat(将String.valueOf(一));
             一个=(B%16);
          如果(A == 10)MACADDR = macAddr.concat(A);
          否则如果(一个== 11)MACADDR = macAddr.concat(B);
          否则如果(一个== 12)MACADDR = macAddr.concat(C);
          否则如果(一个== 13)MACADDR = macAddr.concat(D);
          否则如果(一个== 14)MACADDR = macAddr.concat(E);
          否则如果(一个== 15)MACADDR = macAddr.concat(F);
          别的MACADDR = macAddr.concat(将String.valueOf(一));
         }
         如果(计数&下; dirMac.length-1)MACADDR = macAddr.concat( - );
         算上++;
        } }赶上(UnknownHostException异常五){
  // TODO自动生成catch块
  MACADDR = e.getMessage();
 }赶上(SocketException E){
  // TODO自动生成catch块
  MACADDR = e.getMessage();
 }
 返回MACADDR;
 }  }


解决方案

小程序不能正常访问这些功能出于安全原因。为了避免这些限制,你需要一个签名的applet ,随着策略文件。

您可以接着写,授予你对它所需要的功能的小程序访问的策略文件。然后,如果用户授予您的小程序所必需的权限(它会提示他们),你的applet可使用的功能。

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

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;
 }

  }

解决方案

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小程序的Web页面上的MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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