gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:18:29 25 4
gpt4 key购买 nike

我想创建一个应用程序,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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com