我正在尝试制作一个 JAVA Applet 来检索用户的 MAC 地址,将其转换为 MD5 并将其发送到 javascript 函数,以便它可以做一些事情。
我打算将此 MD5 与其他一些输入数据一起附加到表单中。
到目前为止,我可以检索 MAC 地址并将其成功转换为 MD5。我不能做的是将它传递给我的javascript函数。
这是我的 HTML 代码:
<body>
<script type="text/javascript">
function dohash(hash) { alert(hash); }
</script>
<APPLET code="start.class" width="200" height="200"></APPLET>
</body>
</html>
这是JAVA代码:
import netscape.javascript.JSObject;
import java.applet.*;
import java.security.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.math.BigInteger;
public class start extends Applet {
private static final long serialVersionUID = 1L;
JSObject win;
public void main() {
win = (JSObject)JSObject.getWindow(this);
}
public void init() {
InetAddress ip;
String hashtext = "a";
try {
ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
try {
String plaintext = sb.toString();
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
m.update(plaintext.getBytes());
byte[] digest = m.digest();
BigInteger bigInt = new BigInteger(1,digest);
hashtext = bigInt.toString(16);
while(hashtext.length() < 32 ){
hashtext = "0"+hashtext;
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
win.eval("alert('123');");
} catch (NullPointerException e) {
e.printStackTrace();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e){
e.printStackTrace();
}
}
}
这是 Eclipse 控制台上的响应:
java.lang.NullPointerException
at start.init(start.java:50)
at sun.applet.AppletPanel.run(AppletPanel.java:435)
at java.lang.Thread.run(Thread.java:744)