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 ...
随机推荐
- setTimeout闭包常见问题
经常会遇到这样的问题,setTimeout按序输出循环数字,而不是最后输出同一个数字: 题目: for (var i = 0; i < 5; i++) { setTimeout(function ...
- nodejs 热更新插件
键入命令: npm -g install supervisor supervisor必须安装到全局 可以用supervisor 来启动服务 命令supervisor app.js
- 尚硅谷springboot学习17-SpringBoot日志
SpringBoot使用它来做日志功能: <dependency> <groupId>org.springframework.boot</groupId> < ...
- replace()方法解析
search(),match(),用于查找指定字符串返回首次出现的索引值与指定的字符串.这篇我们讲找到指定字符串之后将其替换掉.直接贴: var str="Visit Microsoft!& ...
- java程序员到底该不该了解一点算法(一个简单的递归计算斐波那契数列的案例说明算法对程序的重要性)
为什么说 “算法是程序的灵魂这句话一点也不为过”,递归计算斐波那契数列的第50项是多少? 方案一:只是单纯的使用递归,递归的那个方法被执行了250多亿次,耗时1分钟还要多. 方案二:用一个map去存储 ...
- spring boot 中使用servlet
- JAVA语言 第五周
我准备在下一周对Java语法进行总结,现在写代码模板还要参考,语法掌握的不熟悉. 这一周除了对代码进行完善外,观看了一些java入门学习视频.
- ubuntu下zaibbix3.2报警搭建
1.安装sudo apt install sendmail 2.测试发送邮件: echo "正文!" | mail -s 标题 XXX@qq.com 3.成功后继续安装邮件服务器. ...
- Webpack Getting Started
[Webpack Getting Started] Make sure you have a fresh version of Node.js installed. If you are using ...
- 数据持久化PlayerPrefs
1.Unity3D中的数据持久化是以键值对的形式存储的,可以看作是一个字典 2.Unity3D中的值是通过键名来读取的,当值不存在时,返回默认值 3.在Unity中只支持int.float.strin ...