java获取电脑配置_java获取电脑部分信息

packageutil;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileWriter;importjava.io.InputStreamReader;importjava.net.InetAddress;importjava.net.NetworkInterface;importjava.net.SocketException;importjava.net.UnknownHostException;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.Scanner;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;/*** @todo 获取电脑配置信息

*@authorzhangyanan

* @date 2018年8月6日*/

public classCpuUtil {private static final Logger logger = LoggerFactory.getLogger(CpuUtil.class);/*** @todo 获取电脑cpu序列号

*@authorzhangyanan

* @date 2018年8月6日*/

public staticString getCPUSerial() {

String os= System.getProperty("os.name");if (os.toLowerCase().startsWith("win")) {returnCpuUtil.getWindowsCPUSerial();

}else{returnCpuUtil.getLinuxCPUSerial();

}

}/*** @todo windows获取cpu序列号

*@authorzhangyanan

* @date 2018年8月6日*/

public staticString getWindowsCPUSerial() {

String serial= null;try{

Process process= Runtime.getRuntime().exec(new String[] { "wmic", "cpu", "get", "ProcessorId"});

process.getOutputStream().close();

Scanner sc= newScanner(process.getInputStream());

sc.next();

serial=sc.next();

sc.close();

}catch(Exception e) {

logger.error("getWindowsCPUSerial异常", e);

}returnserial;

}/*** @todo linux获取cpu序列号

*@authorzhangyanan

* @date 2018年8月6日*/

public staticString getLinuxCPUSerial() {

String result= "";try{

File file= File.createTempFile("tmp", ".vbs");

file.deleteOnExit();

FileWriter fw= newjava.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(newInputStreamReader(p.getInputStream()));

String line;while ((line = input.readLine()) != null) {

result+=line;

}

input.close();

file.delete();

}catch(Exception e) {

logger.error("getLinuxCPUSerial异常", e);

}returnresult;

}/*** @todo 获取mac地址

*@authorzhangyanan

* @date 2018年8月6日*/

public staticString getMACAddress() {

InetAddress ia;try{

ia=InetAddress.getLocalHost();returngetMACAddress(ia);

}catch(UnknownHostException e) {

logger.error("getMACAddress()异常", e);return null;

}

}/*** @todo 获取mac地址

*@authorzhangyanan

* @date 2018年8月6日*/

public staticString getMACAddress(InetAddress ia) {//获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。

try{byte[] mac =NetworkInterface.getByInetAddress(ia).getHardwareAddress();//下面代码是把mac地址拼装成String

StringBuffer sb = newStringBuffer();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地址并返回

returnsb.toString().toUpperCase();

}catch(SocketException e) {

logger.error("getMACAddress异常!", e);return null;

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值