java获取电脑部分信息
获取mac地址与cpu序列号
参考博客:https://www.jb51.net/article/94793.htm
另一篇参考地址没记录下来
package util; import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* @todo 获取电脑配置信息
* @author zhangyanan
* @date 2018年8月6日
*/
public class CpuUtil {
private static final Logger logger = LoggerFactory.getLogger(CpuUtil.class);/**
* @todo 获取电脑cpu序列号
* @author zhangyanan
* @date 2018年8月6日
*/
public static String getCPUSerial() {
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("win")) {
return CpuUtil.getWindowsCPUSerial();
} else {
return CpuUtil.getLinuxCPUSerial();
} } /**
* @todo windows获取cpu序列号
* @author zhangyanan
* @date 2018年8月6日
*/
public static String getWindowsCPUSerial() {
String serial = null;
try {
Process process = Runtime.getRuntime().exec(new String[] { "wmic", "cpu", "get", "ProcessorId" });
process.getOutputStream().close();
Scanner sc = new Scanner(process.getInputStream());
sc.next();
serial = sc.next();
sc.close();
} catch (Exception e) {
logger.error("getWindowsCPUSerial异常", e);
}
return serial;
} /**
* @todo linux获取cpu序列号
* @author zhangyanan
* @date 2018年8月6日
*/
public static String getLinuxCPUSerial() {
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"; // + " exit for \r\n" + "Next";
fw.write(vbs);
fw.close();
String path = file.getPath().replace("%20", " ");
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + path);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
file.delete();
} catch (Exception e) {
logger.error("getLinuxCPUSerial异常", e);
} return result;
} /**
* @todo 获取mac地址
* @author zhangyanan
* @date 2018年8月6日
*/
public static String getMACAddress() {
InetAddress ia;
try {
ia = InetAddress.getLocalHost();
return getMACAddress(ia);
} catch (UnknownHostException e) {
logger.error("getMACAddress()异常", e);
return null;
} } /**
* @todo 获取mac地址
* @author zhangyanan
* @date 2018年8月6日
*/
public static String getMACAddress(InetAddress ia) {
// 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
try {
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress(); // 下面代码是把mac地址拼装成String
StringBuffer sb = new StringBuffer(); for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
// mac[i] & 0xFF 是为了把byte转化为正整数
String s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
} // 把字符串所有小写字母改为大写成为正规的mac地址并返回
return sb.toString().toUpperCase();
} catch (SocketException e) {
logger.error("getMACAddress异常!", e);
return null;
}
}
}
java获取电脑部分信息的更多相关文章
- Java获取电脑IP、MAC、各种版本
Java代码获取电脑IP.MAC.各种版本 package com.rapoo.middle.action; import java.io.BufferedReader; import java.io ...
- Java获取电脑硬件信息
package com.szht.gpy.util; import java.applet.Applet; import java.awt.Graphics; import java.io.Buffe ...
- java获取电脑mac物理地址
import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import ...
- java工具类,在Windows,Linux系统获取电脑的MAC地址、本地IP、电脑名
package com.cloudssaas.util; import java.io.BufferedReader; import java.io.IOException; import java. ...
- 使用RXTX获取电脑串口
RXTX是javacomm串口通信的一个扩展 RXTX开发所需文件的下载地址:http://rxtx.qbang.org/wiki/index.php/Download 解压之后可以看到支持各个平台的 ...
- js/java 获取、添加、修改、删除cookie(最全)
一.cookie介绍 1.cookie的本来面目 HTTP协议本身是无状态的.什么是无状态呢,即服务器无法判断用户身份.Cookie实际上是一小段的文本信息(key-value格式).客户端向服务 ...
- java获取https网站证书,附带调用https:webservice接口
一.java 获取https网站证书: 1.创建一个java工程,新建InstallCert类,将以下代码复制进去 package com; import java.io.BufferedReader ...
- C#/VB.NET 获取电脑属性(硬盘ID、硬盘容量、Cpu序列号、MAC地址、系统类型)
在开发过程中,经常需要获取电脑的一些属性,如获取硬盘ID/CPU序列号/MAC地址作为来加密字符串. 1.硬盘 在我查看网上一些文档时,发现很多人对硬盘序列号很模糊~ 什么叫硬盘序列号?指的是作为一个 ...
- java获取图片原始尺寸
java获取图片原始尺寸 URL url = null; InputStream is = null; BufferedImage img = null; try { url = new URL(pi ...
随机推荐
- LeetCode 题解 56. Merge Intervals
题目大意:给出一组区间,合并他们. 首先是排序,首先看start,start小的在前面.start相同的话,end小的在前面. 排序以后,要合并了. 我自己的笨方法,说实在的问题真的很多.提交了好几次 ...
- leetcode 题解: Gray Code
第一眼看到就是枚举,回溯法. n位的ans就是在n-1的ans的基础上,每一个在首位加上1. 但是有个难点,要保证相邻两数之间只有一位在变化,怎么办? 首先 00 00 01 00 01 11 10 ...
- python使用xlrd 操作Excel读写
此文章非本人 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 二.使用介绍 1.导入模块 i ...
- thinkphp中使用phpexecl多表格应用
去PHPExcel官网下载相应的版本,放到thinkphp3.2版本下的ThinkPHP/Library/Vendor/PHPExcel文件夹下 导出表格: //多个单元格(已测试) public ...
- [Shell]一张图知道Shell(图)
- mac使用brew安装sshpass
brew安装sshpass brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Form ...
- shell定时任务crontab
cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # For details ...
- Eclipse绿豆沙护眼
Window-->Preferences-->Editors——>Text Editors —— Background color 背景颜色向你推荐:色调:85.饱和度:123.亮度 ...
- Bootstrap的aria-label和aria-labelledby
[Bootstrap的aria-label和aria-labelledby] 用于盲人阅读的属性,基本也没什么用. 参考:http://blog.csdn.net/liuyan19891230/art ...
- python 进阶(转自http://python.jobbole.com/82633/)
网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pycurl). pycurl – 网络库(绑定libcurl). urllib3 – P ...