Java.Utils:获取电脑配置信息

Java.Utils:获取电脑配置信息packagecom.boob.common.utils;importjava.io.*;/***@description:电脑配置信息*@author:boob*@since:2020/2/7*/publicclassHardwareUtils{publicHardwareUtils(){}/***获取…

大家好,又见面了,我是你们的朋友全栈君。

Don’t say much, just go to the code.

package org.bood.common.utils;

import java.io.*;

/** * 获取电脑配置信息 * * @author bood * @since 2020/10/16 */
public class HardwareUtils { 
   

    private HardwareUtils() { 
   
    }

    /** * <p> * 获取主板序列号 * </p> * * @return:java.lang.String * @author:bood * @date:2020/10/16 */
    public static String getMotherboardSN() { 
   
        String result = "";
        try { 
   
            File file = File.createTempFile("realhowto", ".vbs");
            file.deleteOnExit();
            FileWriter fw = new 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();
    }

    /** * <p> * 获取硬盘序列号 * </p> * * @param drive: 盘符 * @return:java.lang.String * @author:bood * @date:2020/10/16 */
    public static String getHardDiskSN(String drive) { 
   
        String result = "";
        try { 
   
            File file = File.createTempFile("realhowto", ".vbs");
            file.deleteOnExit();
            FileWriter fw = new 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();
    }

    /** * <p> * 获取 CPU 序列号 * </p> * * @return:java.lang.String * @author:bood * @date:2020/10/16 */
    public static String getCPUSerial() { 
   
        String result = "";
        try { 
   
            File file = File.createTempFile("tmp", ".vbs");
            file.deleteOnExit();
            FileWriter fw = new 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();
    }

    /** * <p> * 获取MAC地址,使用前请修改,只适合中文系统,并且名称为以太网适配器的网卡地址 * </p> * * @return:java.lang.String * @author:bood * @date:2020/10/16 */
    @Deprecated
    public static String getMac() { 
   
        String result = "";
        try { 
   

            Process process = Runtime.getRuntime().exec("ipconfig /all");
            InputStreamReader ir = new InputStreamReader(process.getInputStream(), "GBK");
            LineNumberReader input = new LineNumberReader(ir);

            String line;

            while ((line = input.readLine()) != null) { 
   
                if (line.indexOf("以太网适配器") != -1) { 
   
                    while ((line = input.readLine()) != null) { 
   
                        if (line.indexOf("Physical Address") >= 0 || line.indexOf("物理地址") >= 0) { 
   
                            String MACAddr = line.substring(line.indexOf("-") - 2);
                            result = MACAddr;
                            break;
                        }
                    }
                    break;
                }
            }
        } catch (IOException e) { 
   
            System.err.println("IOException " + e.getMessage());
        }
        return result;
    }

}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159024.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SQL左连接、右连接、笛卡尔积的表现形式「建议收藏」

    表A记录如下:aIDaNum1a200501112a200501123a200501134a200501145a20050115表B记录如下:bIDbName12006032401

    2022年4月9日
    78
  • vmware15win7能用吗_虚拟机重装系统

    vmware15win7能用吗_虚拟机重装系统step1:打开Vmware15虚拟机工具step2:新建虚拟机step3:初学者建议选择典型配置step4:安装操作系统,选择物理机上操作系统安装的光盘文件step5:此次安装的是NelKylin7系统,内核是Linux系统step6:命名你想要的虚拟机名称和存放的内存位置step7:设置磁盘大小(不宜过小),初学者建议将虚拟机存储为单个文件step8:点击完成,虚拟机就创建好了step9:开启虚拟机,完成操作系统的初始化step10:安装系统,点击enterstep11:选择各项

    2022年8月10日
    58
  • linux终端定时器实验报告,定时器实验报告.doc

    linux终端定时器实验报告,定时器实验报告.doc定时器实验报告实验六 定时器/计数器一、实验目的⒈ 学会8253芯片和微机接口的原理和方法。掌握8253定时器/计数器的工作方式和编程原理。二、实验内容用8253的0通道工作在方式3,产生方波。三、实验接线图四、编程指南⒈ 8253芯片介绍8253是一种可编程定时/计数器,有三个十六位计数器,其计数频率范围为0-2MHz,用+5V单电源供电。8253的功能用途:延时中断 实时时钟可编程频率发…

    2022年7月26日
    15
  • C语言单链表的基本操作总结(增删改查)「建议收藏」

    C语言单链表的基本操作总结(增删改查)「建议收藏」1.链表概述  链表是一种常见的数据结构。它与常见的数组是不同的,使用数组时先要指定数组包含元素的个数,即为数组的长度,但是如果向这个数组中加入的元素超过了数组的大小时,便不能将内容全部保存。  链表这种存储方式,其元素个数是不受限定的,当进行添加元素的时候存储的个数就会随之改变。  链表一般有两种形式,有空头链表和无空头链表。  在链表中有一个头指针变量,这个指针变量保存一个地址,…

    2022年6月16日
    36
  • 谷粒商城笔记-基础篇-2(2/4)

    谷粒商城笔记-基础篇-2(2/4)1.整体介绍1)安装vagrant2)安装Centos7$vagrantinitcentos/7A`Vagrantfile`hasbeenplacedinthisdirectory.Youarenowreadyto`vagrantup`yourfirstvirtualenvironment!PleasereadthecommentsintheVagrantfileaswellasdocumentationon`vagrantup

    2022年6月5日
    34
  • anaconda pycharm设置编译器_anaconda pycharm环境配置

    anaconda pycharm设置编译器_anaconda pycharm环境配置Pycharm是一个非常好用的Python编译运行IDE,anaconda则用于管理Python中各种有用的包。下面讲讲在Ubuntu系统下让Pycharm能够使用anaconda管理的各种包。1找到编译器选项首先打开Pycharm然后点击File-&amp;amp;amp;amp;amp;gt;settings,然后就可以看到下图所示界面:…

    2022年8月26日
    19

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号