public class HardWareUtils {

/**   *   * 获取主板序列号   *   *   *   * @return   */

public static String getMotherboardSN() {

String result = "";

try {

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"

+ "    exit for  ' 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()));

String line;

while ((line = input.readLine()) != null) {

result += line;

}

input.close();

} catch (Exception e) {

e.printStackTrace();

}

return result.trim();

}

/**   *   * 获取硬盘序列号   *   *   *   * @param drive   *   *            盘符   *   * @return   */

public static String getHardDiskSN(String drive) {

String result = "";

try {

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()));

String line;

while ((line = input.readLine()) != null) {

result += line;

}

input.close();

} catch (Exception e) {

e.printStackTrace();

}

return result.trim();

}

/**   *   * 获取CPU序列号   *   *   *   * @return   */

public static String getCPUSerial() {

String result = "";

try {

File file = File.createTempFile("tmp", ".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_Processor\") \n"

+ "For Each objItem in colItems \n"

+ "    Wscript.Echo objItem.ProcessorId \n"

+ "    exit for  ' do the first cpu only! \n" + "Next \n";

// + "    exit for  \r\n" + "Next";

fw.write(vbs);

fw.close();

Process p = Runtime.getRuntime().exec(

"cscript //NoLogo " + file.getPath());

BufferedReader input = new BufferedReader(new InputStreamReader(

p.getInputStream()));

String line;

while ((line = input.readLine()) != null) {

result += line;

}

input.close();

file.delete();

} catch (Exception e) {

e.fillInStackTrace();

}

if (result.trim().length() < 1 || result == null) {

result = "无CPU_ID被读取";

}

return result.trim();

}

/**   *   * 获取MAC地址   */

public static String getMac() {

String result = "";

try {

Process process = Runtime.getRuntime().exec("ipconfig");// /all");

InputStreamReader ir = new InputStreamReader(

process.getInputStream());

LineNumberReader input = new LineNumberReader(ir);

String line;

while ((line = input.readLine()) != null)

if (line.indexOf("Physical Address") > 0||line.indexOf("物理地址") > 0) {

String MACAddr = line.substring(line.indexOf("-") - 2);

result = MACAddr;

}

} catch (java.io.IOException e) {

System.err.println("IOException " + e.getMessage());

}

return result;

}

public static void main(String[] args) {

System.out.println("CPU  SN:" + HardWareUtils.getCPUSerial());

System.out.println("主板  SN:" + HardWareUtils.getMotherboardSN());

System.out.println("C盘   SN:" + HardWareUtils.getHardDiskSN("c"));

System.out.println("MAC  SN:" + HardWareUtils.getMac());

}

}

注:获取MAC的方法有问题,其思路是执行ipconfig /all命令并获取输出结果中的物理地址的值,但是在win7上测试时,ipconfig /all命令的输出结果中有多个物理地址,代码中取得的是最后一个地址,其准确性有待考证。另:仅适用于windows系统,linux时需要修改代码,如linux下查看ip地址的命令是ifconfig而不是ipconfig。

java获取计算机硬件参数的更多相关文章

  1. Java获取函数参数名称

    原理 编译之后的class文件默认是不带有参数名称信息的,使用 IDE 时,反编译jar包得到的源代码函数参数名称是 arg0,arg1......这种形式,这是因为编译 jar 包的时候没有把符号表 ...

  2. Java获取方法参数名、Spring SpEL解析

    @Test public void testParse() { //表达式解析 ExpressionParser expressionParser = new SpelExpressionParser ...

  3. Java 获取url参数

    1. 方式一:使用HttpServletRequest对象 String id = arg0.getParameter("id"); mv.addObject("id&q ...

  4. java获取request中的参数、java解析URL问号后的参数

    java获取request中的参数.java解析URL问号后的参数.有时候我们需要从request中获取参数,或者获取拼接在Url后面的参数,有时候一个一个去拿有点麻烦,一起拿出来放在一个map里面需 ...

  5. java 获取url及url参数解析

    java  获取url及url参数解析 一.url编码:URLEncoder.encode(userName); 二.url解码: URLDecoder.decode(userName);

  6. 【java】java获取JVM启动参数 System.getProperty

    java获取JVM启动参数 System.getProperty取 -D后的key即可 public class Test { public static void main(String[] arg ...

  7. java 获取request中的请求参数

    1.get 和 post请求方式 (1)request.getParameterNames(); 获取所有参数key后.遍历request.getParameter(key)获取value (2)re ...

  8. java获取https网站证书,附带调用https:webservice接口

    一.java 获取https网站证书: 1.创建一个java工程,新建InstallCert类,将以下代码复制进去 package com; import java.io.BufferedReader ...

  9. 学习SpringMVC——如何获取请求参数

    @RequestParam,你一定见过:@PathVariable,你肯定也知道:@QueryParam,你怎么会不晓得?!还有你熟悉的他(@CookieValue)!她(@ModelAndView) ...

随机推荐

  1. ZeroBraneStudio之支持GBK文件编码

    费了好大劲终于搞定了让ZBS支持打开GBK文件了.记录下过程: 看源码发现ZBS打开文件时会调用src\editor\commands.lua中的LoadFile函数,代码如下: local file ...

  2. iptables 配置需要保存

    iptables-save > /root/myiptables 将iptables规则导入到文件/root/myiptablse iptables-restore < /root/myi ...

  3. javascript学习代码-判断闰年

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 【技术贴】SqlServer2008 R2 安装失败提示出现以下错误 服务 MSSQLSERVERO

    Feature: Analysis Services  Status: 失败: 请查看日志了解详细信息  MSI status: 已通过  Configuration status: 失败: 请查看下 ...

  5. Junit4学习笔记

    一.初始化标注 在老Junit4提供了setUp()和tearDown(),在每个测试函数调用之前/后都会调用. @Before: Method annotated with @Before exec ...

  6. 并行HASH JOIN小表广播问题

    SQL语句: SELECT /*+parallel(t1 16)*/ T1.DATA_DATE, T1.ACCT_NO, T1.ACCT_ORD, T1.ACCT_NO_PK, T1.ACCT_BAL ...

  7. netstat 命令state值

    1.LISTENING状态 FTP服务启动后首先处于侦听(LISTENING)状态. State显示是LISTENING时表示处于侦听状态,就是说该端口是开放的,等待连接,但还没有被连接.就像你房子的 ...

  8. windows下安装python的C扩展编译环境(解决“Unable to find vcvarsall.bat”)

    个人文章除注明转载外,均为个人原创或者翻译. 个人文章欢迎各种形式的转载,但请18岁以上的转载者注明文章出处,尊重我的劳动,也尊重你的智商: 本文链接:http://www.cnblogs.com/f ...

  9. xgboost在windows上的安装

    xgboost是一个boosting+decision trees的工具包,看微博上各种大牛都说效果很好,于是下载一个,使用了一下,安装步骤如下. 第一步,编译生成xgboost.exe(用于CLI) ...

  10. lost connection to mysql server reading initial communication packet