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


相关推荐

  • Linux04:(4.6k)vim编辑器「建议收藏」

    Linux04:(4.6k)vim编辑器「建议收藏」文章目录Linux_day04一.vim编辑器vim的三种模式1.命令模式2.末行模式3.编辑模式实用功能扩展内容==1.vim的配置文件==2.异常退出问题3.别名机制4.退出方式补充一些win10下的快捷键Linux_day04一.vim编辑器vim的三种模式命令模式不能对文件直接编辑,但可以通过快捷键删除行,复制,粘贴,移动光标等编辑模式-输入末行模式可以在末行输入命令:搜索,替换,保存,退出,撤销vim打开文件的方式:1.#vim 文件路径——直接打开文件(光

    2022年8月9日
    5
  • vlan的原理和作用_生态因子作用的基本原理

    vlan的原理和作用_生态因子作用的基本原理相关名词:VirtualLocalAreaNetwork—VLAN、VirtualPrivateNetwork—VPN、VirtualRedundancyProtocol—VRRP等。一。VLAN的作用1.把一个广播帧所能到达的整个范围称为二层广播域,简称广播域。显然,一个交换网络其实就是一个广播域。在该网络中,假定PC0向PC10发送一个单播帧Y…

    2022年8月10日
    8
  • ZenCart模板结构说明

    ZenCart模板结构说明index.php主文件includes/templates/[customtemplatefolder]/common/html_header.php页面的head部分includes/templates/[customtemplatefolder]/common/tpl_main_page.php页面的body部分includes/templates/[customtemplate

    2022年7月27日
    8
  • 美女图片采集器 (源码+解析)[通俗易懂]

    美女图片采集器 (源码+解析)[通俗易懂]前言:有一段时间没写博客了,”持之以恒”徽章都暗了,实在不该。前一段确实比较忙,…小小地给自己的懒找个借口吧。大二即将结束,学习iOS也有一段时间了。今天抽点时间,开源一个前几天刚上传的App里面的一个功能,RT,美女图片采集器。 美女..相信没有人不喜欢吧,基于此,这个小Demo应运而生。注: 本文正在参加博

    2025年11月14日
    3
  • OJ术语: AC、WA、TLE、OLE、MLE、RE、PE、CE「建议收藏」

    OJ术语: AC、WA、TLE、OLE、MLE、RE、PE、CE「建议收藏」起因看到一些术语不清楚是什么意思,上网查阅相关资料后,归纳如下。汇总简写全称中文称谓OJOnlineJudge在线判题系统ACAccepted通过WAWrongAnswer答案错误TLETimeLimitExceed超时OLEOutputLimitExceed超过输出限制MLE

    2022年6月15日
    82
  • 【c#】DataTable分页处理

    【c#】DataTable分页处理【c#】DataTable分页处理

    2022年4月25日
    42

发表回复

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

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