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;
}
}
}