欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java獲取機(jī)器碼簡單實(shí)現(xiàn)demo

 更新時(shí)間:2023年11月21日 10:45:44   作者:初窺門徑  
這篇文章主要為大家介紹了java獲取機(jī)器碼的簡單實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

java獲取機(jī)器碼

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;
/**
 * @author shanjunpeng
 * @date 2023/9/27
 */
public class MachineCodeUtils {
    public static final String WINDOWS = "Windows";
    public static final String LINUX = "Linux";
    /**
     * 獲取機(jī)器碼
     */
    public static String getMachineCode() {
        return getMachineCode(getOS());
    }
    /**
     * 獲取操作系統(tǒng)
     * @return
     */
    public static String getOS() {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("win")) {
            return WINDOWS;
        } else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) {
            return LINUX;
        } else if (os.contains("mac")) {
            return "Mac OS";
        } else if (os.contains("sunos")) {
            return "Solaris";
        } else {
            return "Unknown";
        }
    }
    public static String getMachineCode(String type) {
        if (Objects.isNull(type)) {
            return "";
        }
        Map<String, Object> codeMap = new HashMap<>(2);
        String result = "";
        if (LINUX.equals(type)) {
            String boisVersion = getBoisVersion();
            codeMap.put("boisVersion", boisVersion);
            System.out.println("boisVersion:" + boisVersion);
            String uuid = getUUID();
            codeMap.put("uuid", uuid);
            System.out.println("uuid:" + uuid);
        } else if (WINDOWS.equals(type)) {
            String processorId = getCPUSerialNumber();
            codeMap.put("ProcessorId", processorId);
            System.out.println("ProcessorId:" + processorId);
            String serialNumber = getHardDiskSerialNumber();
            codeMap.put("SerialNumber", serialNumber);
            System.out.println("SerialNumber:" + serialNumber);
        }else{
            return "";
        }
        String codeMapStr = JSON.toJSONString(codeMap);
        String serials = DigestUtil.md5Hex(codeMapStr);
        result = getSplitString(serials, "-", 4);
        return result.toUpperCase();
    }
    public static String getSplitString(String str, String joiner, int number) {
        StringBuilder sb = new StringBuilder();
        int len = str.length();
        for (int i = 0; i < len; i += number) {
            if (i + number <= len) {
                sb.append(str, i, i + number);
            } else {
                sb.append(str.substring(i));
            }
            if (i + number < len) {
                sb.append(joiner);
            }
        }
        return sb.toString();
    }
    /**
     * 獲取CPU序列號
     *
     * @return
     * @throws IOException
     */
    public static String getCPUSerialNumber() {
        String next;
        try {
            Process process = Runtime.getRuntime().exec(new String[]{"wmic", "cpu", "get", "ProcessorId"});
            process.getOutputStream().close();
            Scanner sc = new Scanner(process.getInputStream());
            String serial = sc.next();
            next = sc.next();
        } catch (IOException e) {
            throw new RuntimeException("獲取CPU序列號失敗");
        }
        return next;
    }
    /**
     * 獲取 硬盤序列號(Windows)
     *
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static String getHardDiskSerialNumber() {
        try {
            Process process = Runtime.getRuntime().exec(new String[]{"wmic", "path", "win32_physicalmedia", "get", "serialnumber"});
            process.getOutputStream().close();
            Scanner sc = new Scanner(process.getInputStream());
            String serial = sc.next();
            return sc.next();
        } catch (IOException e) {
            throw new RuntimeException("獲取硬盤序列號失敗");
        }
    }
    /**
     * 獲取系統(tǒng)序列號(linux)
     *
     * @return
     */
    public static String getUUID() {
        String result = "";
        try {
            Process process = Runtime.getRuntime().exec("sudo dmidecode -s system-uuid");
            InputStream in;
            BufferedReader br;
            in = process.getInputStream();
            br = new BufferedReader(new InputStreamReader(in));
            while (in.read() != -1) {
                result = br.readLine();
            }
            br.close();
            in.close();
            process.destroy();
            System.out.println("獲取序列號:" + result);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * bois版本號(linux)
     *
     * @return
     */
    public static String getBoisVersion() {
        String result = "";
        Process p;
        try {
            // 管道
            p = Runtime.getRuntime().exec("sudo dmidecode -s bios-version");
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                result += line;
                break;
            }
            br.close();
        } catch (IOException e) {
            System.out.println("獲取主板信息錯(cuò)誤");
        }
        return result;
    }
}

以上就是java獲取機(jī)器碼實(shí)現(xiàn)demo的詳細(xì)內(nèi)容,更多關(guān)于java獲取機(jī)器碼的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Gradle 6.6.1 安裝配置的詳細(xì)教程

    Gradle 6.6.1 安裝配置的詳細(xì)教程

    Gradle是一個(gè)基于Apache Ant和Apache Maven概念的項(xiàng)目自動(dòng)化構(gòu)建開源工具。這篇文章主要介紹了Gradle 6.6.1 安裝配置的詳細(xì)教程,需要的朋友可以參考下
    2020-09-09
  • MySQL查詢字段實(shí)現(xiàn)字符串分割split功能的示例代碼

    MySQL查詢字段實(shí)現(xiàn)字符串分割split功能的示例代碼

    本文主要介紹了MySQL查詢字段實(shí)現(xiàn)字符串分割split功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Java語言多線程終止中的守護(hù)線程實(shí)例

    Java語言多線程終止中的守護(hù)線程實(shí)例

    這篇文章主要介紹了Java語言多線程終止中的守護(hù)線程實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2017-12-12
  • Java中網(wǎng)絡(luò)IO的實(shí)現(xiàn)方式(BIO、NIO、AIO)介紹

    Java中網(wǎng)絡(luò)IO的實(shí)現(xiàn)方式(BIO、NIO、AIO)介紹

    這篇文章主要介紹了Java中網(wǎng)絡(luò)IO的實(shí)現(xiàn)方式(BIO、NIO、AIO)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Java中的SimpleDateFormat的線程安全問題詳解

    Java中的SimpleDateFormat的線程安全問題詳解

    這篇文章主要介紹了Java中的SimpleDateFormat的線程安全問題詳解,sonar 是一個(gè)代碼質(zhì)量管理工具,SonarQube是一個(gè)用于代碼質(zhì)量管理的開放平臺(tái),為項(xiàng)目提供可視化報(bào)告, 連續(xù)追蹤項(xiàng)目質(zhì)量演化過程,需要的朋友可以參考下
    2024-01-01
  • 簡單了解SpringMVC與Struts2的區(qū)別

    簡單了解SpringMVC與Struts2的區(qū)別

    這篇文章主要介紹了簡單了解SpringMVC與Struts2的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • 全面解析Java中的注解與注釋

    全面解析Java中的注解與注釋

    這篇文章主要介紹了Java中的注解與注釋,簡單來說注解以@符號開頭而注釋被包含在/***/符號中,各自具體的作用則來看本文詳解,需要的朋友可以參考下
    2016-05-05
  • Java 批量刪除html中注釋內(nèi)容的方法

    Java 批量刪除html中注釋內(nèi)容的方法

    最近項(xiàng)目中有一個(gè)功能需要讀取外部html文本文件。但是有的html文件里面有大量的注釋,需要?jiǎng)h除其中的注釋在存儲(chǔ)
    2014-04-04
  • 詳解JAVA Timer和TimerTask

    詳解JAVA Timer和TimerTask

    這篇文章主要介紹了JAVA Timer和TimerTask的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • SpringBoot HttpMessageConverter消息轉(zhuǎn)換器的使用詳解

    SpringBoot HttpMessageConverter消息轉(zhuǎn)換器的使用詳解

    在整個(gè)數(shù)據(jù)流轉(zhuǎn)過程中,前端的請求報(bào)文轉(zhuǎn)化為Java對象,Java對象轉(zhuǎn)化為響應(yīng)報(bào)文,這里就用到了消息轉(zhuǎn)換器HttpMessageConverter
    2022-06-06

最新評論