仅通过JS实现,适用于IE10+浏览器
<script>
function getSerialNumber() {
var retVal = "";
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_DiskDrive");
var enu = new Enumerator (properties);
for (;!enu.atEnd();enu.moveNext ()){
var p = enu.item ();
if(retVal!=‘‘){
retVal += ",";
}
retVal += p.SerialNumber;
}
return retVal;
}
document.write("硬盘号为:"+getSerialNumber());
</script>
//************************************************************************************************************
JavaApplet实现:
import java.applet.Applet;
import java.awt.Graphics;
import java.io.IOException;
import java.util.Scanner;
public class HelloApplet extends Applet {
private static final long serialVersionUID = 5511892956119084309L;
@Override
public void paint(Graphics g) {
g.drawString(getDriveSerialNumber(), 10, 10);
}
public String getDriveSerialNumber(){
String retValue = "";
try {
Process process = Runtime.getRuntime().exec("wmic diskdrive get serialnumber");
try {
process.waitFor();
} catch (InterruptedException e) {
//
}
Scanner scanner = new Scanner(process.getInputStream());
while (scanner.hasNext()) {
String line = scanner.next();
if ("SerialNumber".equals(line)) {
continue;
}
if (retValue.length() > 0) {
retValue += ",";
}
retValue += line;
}
scanner.close();
if (retValue.length() == 0) {
retValue = "Failed to get serialnumber";
}
} catch (IOException e) {
e.printStackTrace();
retValue = "Error: " + e.getMessage();
}
return retValue;
}
}
//===========================html部分
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Example of serialnumber-applet.</title> </head>
<body>
<object classid="clsid:CAFEEFAC-0016-0000-FFFF-ABCDEFFEDCBA"
type="application/x-java-applet" name="diskSerialNumber"
style="position: absolute; top: -1000px; left: -1000px;">
<param name="code" value="HelloApplet ">
<param name="archive" value="final.jar">
<param name="mayscript" value="true">
<param name="scriptable" value="true">
<param name="width" value="0">
<param name="height" value="0">
</object>
<script type="text/javascript">
if (typeof (document.diskSerialNumber.isActive) == "undefined") {
document.write("Unable to detect serialnumber.");
} else {
var serialnumber = document.diskSerialNumber.getDriveSerialNumber();
if (serialnumber.length > 0) {
document.write("硬盘:<br />");
document.write("<hr>");
document.write(serialnumber);
}
}
</script>
</body>
</html>
//applet签名
可参考:http://my.oschina.net/u/204292/blog/403337
JS与applet交互可参考:http://my.oschina.net/penngo/blog/112513