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. 练习2 J题 - 多项式求和

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 多项式 ...

  2. iOS 仪表式数字跳动动画-b

    前几天搞了 双曲线波浪动画(http://www.jianshu.com/p/7db295fd38eb)和环形倒计时动画(http://www.jianshu.com/p/d1d16dff33c9)而 ...

  3. JavaScript中url 传递参数(特殊字符)解决方法

    有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了.下表中列出了一些URL特殊符号及编码 十六进制值1. + URL 中+号表示空格 %2B2. 空格 UR ...

  4. OpenJudge_cdqz 数据结构版块小结

    题目整理 Challenge 0  随机线性存储表-easy Challenge 1  链表数组-easy Challenge 2  可持久化Treap的可持久化运用-hard Challenge 3 ...

  5. 安卓天天练练(五)CompoundButton

    ToggleButton 让我想起了从前jQuery还没有取消toggle方法时是怎么偷懒的.. 注意: 如果LinearLayout,与RelativeLayout不同,必须有orientation ...

  6. Hibernate 缓存 关于注解方式

    要引入 import org.hibernate.annotations.Cache; 在类前面添加: @Cache(usage= CacheConcurrencyStrategy.NONSTRICT ...

  7. [转载]# Ajax异步请求阻塞情况的解决办法

    最近使用ExtJs4的mvc模式在开发了在线漫画的后台,因为异步请求比较多,有的回应时间长,有点短.我发现在多次并发的情况下,会造成阻塞的情况.也就是说如果回应时间长的请求还在进行中,短的请求却被挂起 ...

  8. 【POJ1470】Closest Common Ancestors

    Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For e ...

  9. I2C总线之(三)---以C语言理解IIC

    为了加深对I2C总线的理解,用C语言模拟IIC总线,边看源代码边读波形: 如下图所示的写操作的时序图: 读时序的理解同理.对于时序不理解的朋友请参考“I2C总线之(二)---时序” 完整的程序如下: ...

  10. Weblogic8.1 的性能优化

    注:在下面做的介绍都是以Weblogic8.1为例的,其它版本的Weblogic可能会有些许不同. 1) 设置JAVA参数: a) 编辑Weblogic Server启动脚本文件: BEA_HOMEu ...