String requestTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
package com.picc.pay.openapi;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
public class MacUtil {
public static String getMacByIP() throws Exception {
return getMacByIP(InetAddress.getLocalHost().getHostAddress());
}
public static String getMacByIP(String IP) throws Exception {
InetAddress ia = InetAddress.getByName(IP);
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
String hexString = Integer.toHexString(mac[i] & 0xFF);
sb.append(hexString.length() == 1 ? "0" + hexString : hexString);
}
return sb.toString().toUpperCase();
}
public static String getCPUSerial() {
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";
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) {
e.fillInStackTrace();
}
if (result.trim().length() < 1 || result == null) {
result = "无CPU_ID被读取";
}
return result.trim();
}
}
InetAddress addr = InetAddress.getLocalHost();
System.out.println("本机主机端口号:"+addr);
String macByIP = MacUtil.getMacByIP();
System.out.println("获取本机mac接口:"+macByIP);
String cpuSerial = MacUtil.getCPUSerial();
System.out.println("获取本机CPU序列号:"+cpuSerial);