请教高手:java如何读java 获取硬盘序列号的序列号

博客分类:
When you need to know hardware details, Java is not the best tool unless you call a JNI routine or an external utility. The JNI solution is always the best because it is designed to interact closely with Java but it may be more complex to develop. If your need is simple (no interaction) and the need to be cross plateform is not present then calling an external utility is maybe "a good enough" choice.
如上所述,要想获取硬件信息,java不是最好的方式。。。
In these 2 examples, we create the appropriate VBS script file on-the-fly and capture its output. They are very Windows oriented since they rely on the "Windows Script Host" to execute the generated scripts.
The vbscript queries a WMI class to get a specific hardware information. Here we are using the Win32_BaseBoard but they are many others, see /en-us/library/aa389273.aspx for complete list.
Motherboard serial number
//通过vbscript的方式获取主板序列号
import java.io.F
import java.io.FileW
import java.io.BufferedR
import java.io.InputStreamR
public class MiscUtils {
private MiscUtils() {
public static String getMotherboardSN() {
String result = "";
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs =
"Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
(\"Select * from Win32_BaseBoard\") \n"
+ "For Each objItem in colItems \n"
Wscript.Echo objItem.SerialNumber \n"
' do the first cpu only! \n"
+ "Next \n";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
input.close();
catch(Exception e){
e.printStackTrace();
return result.trim();
public static void main(String[] args){
String cpuId = MiscUtils.getMotherboardSN();
javax.swing.JOptionPane.showConfirmDialog((ponent)
null, cpuId, "Motherboard serial number",
javax.swing.JOptionPane.DEFAULT_OPTION);
Hard disk serial number
//获取硬盘序列号
This serial number is created by the OS where formatting the drive and it's not the manufacturer serial number. It's unique, because it is created on the fly based on the current time information. AFAIK, there is no API that return that the manufacturer SN. At best, the SN of the HD firmware can be read but this will involve some very low-level API calls. Keep in mind that even if you get that number, there is no warranty that it will be unique since each manufacturer can assign the SN as they wish.
import java.io.F
import java.io.FileW
import java.io.BufferedR
import java.io.InputStreamR
public class DiskUtils {
private DiskUtils() {
public static String getSerialNumber(String drive) {
String result = "";
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
+"Set colDrives = objFSO.Drives\n"
+"Set objDrive = colDrives.item(\"" + drive + "\")\n"
+"Wscript.Echo objDrive.SerialNumber";
// see note
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
input.close();
catch(Exception e){
e.printStackTrace();
return result.trim();
public static void main(String[] args){
String sn = DiskUtils.getSerialNumber("C");
javax.swing.JOptionPane.showConfirmDialog((ponent)
null, sn, "Serial Number of C:",
javax.swing.JOptionPane.DEFAULT_OPTION);
import java.io.BufferedR
import java.io.F
import java.io.FileW
import java.io.InputStreamR
import java.net.InetA
import java.net.NetworkI
import java.net.SocketE
import java.net.UnknownHostE
public class UserInfoUtil {
public static String getIp() {
String ip = "";
InetAddress in = InetAddress.getLocalHost();
ip = in.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
* 获取mac地址
public static String getMacAddr() {
String macAddr = "";
addr = InetAddress.getLocalHost();
NetworkInterface dir = NetworkInterface.getByInetAddress(addr);
byte[] dirMac = dir.getHardwareAddress();
int count = 0;
for (int b : dirMac) {
if (b & 0)
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");
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");
macAddr = macAddr.concat(String.valueOf(a));
if (count & dirMac.length - 1)
macAddr = macAddr.concat("-");
} catch (UnknownHostException e) {
macAddr = e.getMessage();
} catch (SocketException e) {
macAddr = e.getMessage();
return macA
* 获取主板序列号
public static String getMotherboardSN() {
String result = "";
File file = File.createTempFile("realhowto", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n" + "Set colItems = objWMIService.ExecQuery _ \n"
(\"Select * from Win32_BaseBoard\") \n" + "For Each objItem in colItems \n" + "
Wscript.Echo objItem.SerialNumber \n"
' do the first cpu only! \n" + "Next \n";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
input.close();
} catch (Exception e) {
e.printStackTrace();
return result.trim();
dreamoftch
浏览: 228360 次
来自: 上海
作者你好,你这个例子中服务端是跑在tomcat中吗
作者你好,能把整个项目给我发一份吗?guotao114@126 ...
楼上 本文提供的代码样例
和 你之前提供的例子有出入好多都没 ...
dannielhome 写道请问下这个upload.exe是什 ...
请问下这个upload.exe是什么东西呢
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'用户名:niliuhe
访问量:4234
注册日期:
阅读量:1297
阅读量:3317
阅读量:584159
阅读量:469445
51CTO推荐博文
&卷的序列号是硬盘格式化时系统随机分配给硬盘的一组十六进制字符串,除分对硬盘重新进行格式化,卷的序列号是不会改变的。所以,很多软件都会以卷的序列号判断用户是否合法用户。以下是JAVA获取卷的序列号的方法。
&public static String getHdSerialInfo() {
& String line = &&;
& String HdSerial = &&;//记录卷的序列号
& &Process proces = Runtime.getRuntime().exec(&cmd /c dir c:&);//获取命令行参数
& &BufferedReader buffreader = new BufferedReader(
& & &new InputStreamReader(proces.getInputStream()));
& &while ((line = buffreader.readLine()) != null) {
& & if (line.indexOf(&卷的序列号是 &) != -1) { &//读取参数并获取卷的序列号
& & &HdSerial = line.substring(line.indexOf(&卷的序列号是 &)
& & & &+ &卷的序列号是 &.length(), line.length());
& & &// System.out.println(HdSerial);
& } catch (IOException e) {
& &// TODO Auto-generated catch block
& &e.printStackTrace();
& return HdS返回卷的序列号
了这篇文章
类别:┆阅读(0)┆评论(0)本帖子已过去太久远了,不再提供回复功能。java 获取CPU 和 硬盘序列号的方法 - CSDN博客
java 获取CPU 和 硬盘序列号的方法
package com.ctne.
* @author guanchen
* @creation 日 上午9:35:43
import java.io.BufferedR
import java.io.F
import java.io.FileW
import java.io.IOE
import java.io.InputStreamR
import java.text.DecimalF
import java.util.S
import org.hyperic.sigar.CpuI
import org.hyperic.sigar.CpuP
import org.hyperic.sigar.S
import org.hyperic.sigar.SigarE
public class Cpu {
private static Sigar sigar = new Sigar();
public static CpuInfo getCpuInfo() {
CpuInfo[] cpuArr = sigar.getCpuInfoList();
return cpuArr[0];
} catch (SigarException e) {
e.printStackTrace();
public static String getCpuPerc() {
CpuPerc[] cpuPercList = sigar.getCpuPercList();
double num = 0.0D;
DecimalFormat df = new DecimalFormat(&#.0&);
for (int i = 0; i & cpuPercList. i++) {
num += cpuPercList[i].getCombined();
if (num == 0.0D) {
return &0&;
return df.format(num * 100.0D);
} catch (SigarException e) {
e.printStackTrace();
return &&;
private static String getHDSerial(String drive) {
String result = &&;
File file = File.createTempFile(&tmp_02&, &.vbs&);
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = &Set objFSO = CreateObject(\&Scripting.FileSystemObject\&)\n&
+ &Set colDrives = objFSO.Drives\n&
+ &Set objDrive = colDrives.item(\&&
+ &Wscript.Echo objDrive.SerialNumber&;
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec(
&cscript //NoLogo & + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
while ((line = input.readLine()) != null) {
input.close();
file.delete();
} catch (Exception e) {
if (result.trim().length() & 1 || result == null) {
result = &&;
return result.trim();
private static String getCPUSerial() {
String result = &&;
File file = File.createTempFile(&tmp_01&, &.vbs&);
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = &On Error Resume Next \r\n\r\n&
+ &strComputer = \&.\&
+ &Set objWMIService = GetObject(\&winmgmts:\& _ \r\n&
& \&{impersonationLevel=impersonate}!\\\\\& & strComputer & \&\\root\\cimv2\&) \r\n&
+ &Set colItems = objWMIService.ExecQuery(\&Select * from Win32_Processor\&)
+ &For Each objItem in colItems\r\n &
Wscript.Echo objItem.ProcessorId
' do the first cpu only! \r\n&
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec(
&cscript //NoLogo & + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
while ((line = input.readLine()) != null) {
input.close();
file.delete();
} catch (Exception e) {
e.fillInStackTrace();
if (result.trim().length() & 1 || result == null) {
result = &&;
return result.trim();
public static String generateMachineNo() throws Exception {
StringBuilder machineNo = new StringBuilder();
String cpuser = getCPUSerial();
machineNo.append(cpuser);
String hdId = getHDSerial(&C:/&);
machineNo.append(hdId);
// String tmp = encrypt(machineNo.toString(), &energyregisterkey&);
return machineNo.toString();
public static String getCPU() {
String serial =
process = Runtime.getRuntime().exec(
new String[] { &wmic&, &cpu&, &get&, &ProcessorId& });
process.getOutputStream().close();
Scanner sc = new Scanner(process.getInputStream());
String property = sc.next();
serial = sc.next();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
public static void main(String[] args) throws Exception {
String sigar = Cpu.getCpuInfo().getModel();
System.out.println(&sigar 获取 : & + sigar);
System.out.println(&java 获取 : & + getCPU());
String vbs =
vbs = generateMachineNo();
System.out.println(&VB 获取 : & + vbs);
} catch (Exception e) {
e.printStackTrace();
本文已收录于以下专栏:
相关文章推荐
希捷(Seagate)迈拓(Maxtor)西部数据(Western Digital)三星(SAMSUNG) 日立(HITACHI)等硬盘都有自己规定方式编号的硬盘序列号,所谓序列号就是硬盘编号。硬盘序...
java 获取主板编号
import java.io.F
import java.io.FileW
import java.io.BufferedR
import ...
一个简单的小工具,用java执行系统命令,并打印输出。public class OSExecute
* command。
* 详细说明:
* 无。
...
硬盘序列号是硬盘格式化时系统随机分配给硬盘的一组十六进制字符串,除分对硬盘重新进行格式化,硬盘序列号是不会改变的。所以,很多软件都会以硬盘序列号判断用户是否合法用户。以下是JAVA获取硬盘序列号的方法...
import java.io.BufferedR
import java.io.F
import java.io.FileW
import java.io.Inp...
转自:.cn/bbs/thread.jspa?forumID=124&threadID=28320&messageID=171767//Java获得CPU序...
public static void main(String[] args) throws IOException {
        System.out.println(getCPUSN());...
JAVA获取CPU序列号
他的最新文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)

我要回帖

更多关于 读取硬盘序列号 的文章

 

随机推荐