• Java获取电脑硬件信息


      1 package com.szht.gpy.util;
      2 
      3 import java.applet.Applet;
      4 import java.awt.Graphics;
      5 import java.io.BufferedReader;
      6 import java.io.File;
      7 import java.io.FileWriter;
      8 import java.io.InputStreamReader;
      9 import java.net.InetAddress;
     10 import java.net.NetworkInterface;
     11 
     12 
     13 public class HardWareUtils extends Applet {
     14     public HardWareUtils(){
     15     }
     16     private static final long serialVersionUID = 1L;
     17  
     18     @Override
     19     public void paint(Graphics paint) {
     20         super.paint(paint);
     21         paint.drawString("获取硬件信息", 10, 10);
     22         paint.drawString("CPU  SN:" + HardWareUtils.getCPUSerial(), 10, 30);
     23         paint.drawString("主板  SN:" + HardWareUtils.getMotherboardSN(), 10, 50);
     24         paint.drawString("C盘   SN:" + HardWareUtils.getHardDiskSN("c"), 10, 70);
     25         paint.drawString("MAC  SN:" + HardWareUtils.getMac(), 10, 90);
     26     }
     27  
     28     /**
     29      * 获取主板序列号
     30      * 
     31      * @return
     32      */
     33     public static String getMotherboardSN() {
     34         String result = "";
     35         try {
     36             File file = File.createTempFile("realhowto", ".vbs");
     37             file.deleteOnExit();
     38             FileWriter fw = new java.io.FileWriter(file);
     39             String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
     40                     + "Set colItems = objWMIService.ExecQuery _ \n"
     41                     + "   (\"Select * from Win32_BaseBoard\") \n"
     42                     + "For Each objItem in colItems \n"
     43                     + "    Wscript.Echo objItem.SerialNumber \n"
     44                     + "    exit for  ' do the first cpu only! \n" + "Next \n";
     45  
     46             fw.write(vbs);
     47             fw.close();
     48             Process p = Runtime.getRuntime().exec(
     49                     "cscript //NoLogo " + file.getPath());
     50             BufferedReader input = new BufferedReader(new InputStreamReader(p
     51                     .getInputStream()));
     52             String line;
     53             while ((line = input.readLine()) != null) {
     54                 result += line;
     55             }
     56             input.close();
     57         } catch (Exception e) {
     58             e.printStackTrace();
     59         }
     60         return result.trim();
     61     }
     62  
     63     /**
     64      * 获取硬盘序列号
     65      * 
     66      * @param drive
     67      *            盘符
     68      * @return
     69      */
     70     public static String getHardDiskSN(String drive) {
     71         String result = "";
     72         try {
     73             File file = File.createTempFile("realhowto", ".vbs");
     74             file.deleteOnExit();
     75             FileWriter fw = new java.io.FileWriter(file);
     76  
     77             String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
     78                     + "Set colDrives = objFSO.Drives\n"
     79                     + "Set objDrive = colDrives.item(\""
     80                     + drive
     81                     + "\")\n"
     82                     + "Wscript.Echo objDrive.SerialNumber"; // see note
     83             fw.write(vbs);
     84             fw.close();
     85             Process p = Runtime.getRuntime().exec(
     86                     "cscript //NoLogo " + file.getPath());
     87             BufferedReader input = new BufferedReader(new InputStreamReader(p
     88                     .getInputStream()));
     89             String line;
     90             while ((line = input.readLine()) != null) {
     91                 result += line;
     92             }
     93             input.close();
     94         } catch (Exception e) {
     95             e.printStackTrace();
     96         }
     97         return result.trim();
     98     }
     99  
    100     /**
    101      * 获取CPU序列号
    102      * 
    103      * @return
    104      */
    105     public static String getCPUSerial() {
    106         String result = "";
    107         try {
    108             File file = File.createTempFile("tmp", ".vbs");
    109             file.deleteOnExit();
    110             FileWriter fw = new java.io.FileWriter(file);
    111             String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
    112                     + "Set colItems = objWMIService.ExecQuery _ \n"
    113                     + "   (\"Select * from Win32_Processor\") \n"
    114                     + "For Each objItem in colItems \n"
    115                     + "    Wscript.Echo objItem.ProcessorId \n"
    116                     + "    exit for  ' do the first cpu only! \n" + "Next \n";
    117             fw.write(vbs);
    118             fw.close();
    119             Process p = Runtime.getRuntime().exec(
    120                     "cscript //NoLogo " + file.getPath());
    121             BufferedReader input = new BufferedReader(new InputStreamReader(p
    122                     .getInputStream()));
    123             String line;
    124             while ((line = input.readLine()) != null) {
    125                 result += line;
    126             }
    127             input.close();
    128             file.delete();
    129         } catch (Exception e) {
    130             e.fillInStackTrace();
    131         }
    132         if (result.trim().length() < 1 || result == null) {
    133             result = "无CPU_ID被读取";
    134         }
    135         return result.trim();
    136     }
    137  
    138     /**
    139      * 获取MAC地址
    140      */
    141     public static String getMac() {
    142         try {
    143             byte[] mac = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();
    144             StringBuffer sb = new StringBuffer();
    145             for (int i = 0; i < mac.length; i++) {
    146                 if (i != 0) {
    147                     sb.append("-");
    148                 }
    149                 String s = Integer.toHexString(mac[i] & 0xFF);
    150                 sb.append(s.length() == 1 ? 0 + s : s);
    151             }
    152             return sb.toString().toUpperCase();
    153         } catch (Exception e) {
    154             return "";
    155         }
    156  
    157     }
    158 
    159     
    160     public static void main(String[] args) throws Exception {
    161         System.out.println(getCPUSerial());//CPU
    162         System.out.println(getMotherboardSN());//主板
    163         System.out.println(getHardDiskSN("c"));//c盘
    164         System.out.println(getMac());//MAC
    165         String msg  = getCPUSerial()+getMotherboardSN().replace(".", "")+getHardDiskSN("c")+getMac().replace("-", "");
    166         System.out.println("原始数据:"+msg);
    167         
    168         String encrypt = DesUtil.encrypt(msg);
    169         System.out.println("加密:"+encrypt);
    170         String decrypt = DesUtil.decrypt(encrypt);
    171         System.out.println("解密:"+decrypt);
    172     }
    173 }
  • 相关阅读:
    zookeeper记录2(选举模式和ZooKeeper的集群安装)
    端口复用技术简单了解;重用端口;socket复用端口
    java线程池如何合理配置核心线程数?(转)
    数据库锁机制(转)
    mysql左连接锁表_不得不会的mysql锁(转)
    POI: calculated end index (4361) is out of allowable range (4339..4358)
    jdbcTemplate事务管理
    springboot 访问静态资源
    springboot集成持久化框架
    第六章 FreeBSD之配置日期和时间
  • 原文地址:https://www.cnblogs.com/sunBinary/p/9870535.html
Copyright © 2020-2023  润新知