吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3041|回复: 13
收起左侧

[Java 转载] java 获得计算机硬件信息

  [复制链接]
TeMoon 发表于 2020-12-1 10:10
[Java] 纯文本查看 复制代码
import java.applet.Applet;

import java.awt.*;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileWriter;

import java.io.InputStreamReader;

import java.net.InetAddress;

import java.net.NetworkInterface;



/**

 * @Descirption: 获得电脑硬件信息

 * @Author:WenMo

 * @CreatTime: 2020-11-27 15:39

 */

public class HardWareUtils extends Applet {

    public HardWareUtils(){

    }

    private static final long serialVersionUID = 1L;



    @Override

    public void paint(Graphics paint) {

        super.paint(paint);

        paint.drawString("获取硬件信息", 10, 10);

        paint.drawString("CPU  SN:" + HardWareUtils.getCPUSerial(), 10, 30);

        paint.drawString("主板  SN:" + HardWareUtils.getMotherboardSN(), 10, 50);

        paint.drawString("C盘   SN:" + HardWareUtils.getHardDiskSN("c"), 10, 70);

        paint.drawString("MAC  SN:" + HardWareUtils.getMac(), 10, 90);

    }



    /**

     * 获取主板序列号

     *

     * @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();

    }



    /**

     * 获取硬盘序列号

     *

     * [url=home.php?mod=space&uid=952169]@Param[/url] 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";

            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() {

        try {

            byte[] mac = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();

            StringBuffer sb = new StringBuffer();

            for (int i = 0; i < mac.length; i++) {

                if (i != 0) {

                    sb.append("-");

                }

                String s = Integer.toHexString(mac[i] & 0xFF);

                sb.append(s.length() == 1 ? 0 + s : s);

            }

            return sb.toString().toUpperCase();

        } catch (Exception e) {

            return "";

        }



    }





    public static void main(String[] args) throws Exception {

        System.out.println(getCPUSerial());//CPU

        System.out.println(getMotherboardSN());//主板

        System.out.println(getHardDiskSN("C"));//c盘

        System.out.println(getMac());//MAC

    }

}

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
或跃在渊09 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Kristine_He 发表于 2020-12-1 17:08
看懂了,利用VBS脚本获取的,在做出服务端与客户端,就可以统计公司里面所有机器的硬件信息,用VBS我还没找到获取显示器型号的方法,  驱动精灵是可以获取显示器型号的.
柠檬派 发表于 2021-11-17 13:28
Kristine_He 发表于 2020-12-1 17:08
看懂了,利用VBS脚本获取的,在做出服务端与客户端,就可以统计公司里面所有机器的硬件信息,用VBS我还没找到获 ...

用VBS怎么操作获取呢
luke_huai 发表于 2020-12-1 10:31
我是萌萌哒提莫 发表于 2020-12-1 10:34
大哥  cmd 也能获取呀
SGC沉默 发表于 2020-12-1 10:38
CPU实时hz怎么获取
 楼主| TeMoon 发表于 2020-12-1 10:44

知道呀,这只是在代码里面获得
无敌小车 发表于 2020-12-1 10:53
厉害,学习了。
 楼主| TeMoon 发表于 2020-12-1 11:15
SGC沉默 发表于 2020-12-1 10:38
CPU实时hz怎么获取

这个你还是用sigar吧!
masker_k 发表于 2020-12-1 14:30
感谢楼主无私分享
我是萌萌哒提莫 发表于 2020-12-1 14:52
TeMoon 发表于 2020-12-1 10:44
知道呀,这只是在代码里面获得

我的意思是  runtime 直接执行cmd呀  你还创建个脚本执行干啥呀
wanshiz 发表于 2020-12-1 17:00
一个好思落,谢谢分享。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 10:16

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表