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)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • [029] 微信公众帐号开发教程第5篇-各种消息的接收与响应[通俗易懂]

    [029] 微信公众帐号开发教程第5篇-各种消息的接收与响应[通俗易懂]前一篇文章里我们已经把微信公众平台接口中消息及相关操作都进行了封装,本章节将主要介绍如何接收微信服务器发送的消息并做出响应。明确在哪接收消息从微信公众平台接口消息指南中可以了解到,当用户向公众帐号发消息时,微信服务器会将消息通过POST方式提交给我们在接口配置信息中填写的URL,而我们就需要在URL所指向的请求处理类CoreServlet的doPost方法中接收消息、处理消息和响应

    2022年9月28日
  • ListView控件详解

    ListView控件详解在Windows的资源管理器中,文件夹或文件信息可以分别以大图标或小图标的方式显示如图:这样的洁面效果是如何实现的?这就要用到ImageList控件和ListView控件ImageList控件的属

    2022年7月3日
  • netty pooled vs unpooled ByteBuf

    netty pooled vs unpooled ByteBufWhatsthedifferencebetweenPooledvsUnpooledandDirectvsHeapinByteBuf?Like,whatdoespooledmeansincontextofamessagereceived,becauseobjectlikeHttpRequestiscreatedfromBy…

    2022年11月3日
  • PyCharm如何删除工程项目

    PyCharm如何删除工程项目1、在菜单中选择:file——>closeproject2、选择需要删除的项目右上角的“×”号进行删除工程项目3、找到工程项目的存放路径,删除对应的工程项目文件通过上诉操作即可在pycharm中删除工程文件。转载于:https://www.cnblogs.com/benpao1314/p/9679508.html…

    2022年8月26日
  • 软件架构模式和设计模式的区别_几种常见软件架构

    软件架构模式和设计模式的区别_几种常见软件架构什么是架构?  软件体系结构通常被称为架构,指可以预制和可重构的软件框架结构。架构尚处在发展期,对于其定义,学术界尚未形成一个统一的意见,而不同角度的视点也会造成软件体系结构的不同理解,以下是一些主流的标准观点。ANSI/IEEE610.12-1990软件工程标准词汇对于体系结构定义是:“体系架构是以构件、构件之间的关系、构件与环境之间的关系为内容的某一系统的基本组织结构以及知道上

    2022年10月18日
  • element ui 图片加载失败_vue 3.x 中使用element-ui时, el-image图片加载失败!!「建议收藏」

    element ui 图片加载失败_vue 3.x 中使用element-ui时, el-image图片加载失败!!「建议收藏」问题描述vuecreateele命令创建了一个vue3.x的项目cdelecnpminpmrunserve把项目跑起来cnpmielement-ui-S安装element-ui修改HelloWorld.vue(增加使用el-image及其他组件),跑起来后,el-image对应的图片显示加载失败,但如果换成绝对地址就能正确加载,不知道什么原因!问题出现的环境背景及…

    2022年5月22日

发表回复

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

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