本方法主要用来限制系统在其他的机器上运行其实原理简单的很没有调用第三方插件代码如下:
package comusersutil;
import javaio*;
import javautil*;
import javautilregex*;
public class NetID {
String IPCONFIG_COMMAND_WIN = ipconfig /all;
boolean realMac = true;
String unique = ;
public static String getMacAddress() {
NetID hwid = new NetID();
return hwidgetUnique()trim();
}
private String getUnique() {
String os = SystemgetProperty(osname);
if (osstartsWith(Windows)) {
return getUniqueWindows();
}else {
return ;
}
}
private String getUniqueWindows() {
String ipConfigResponse = null;
try {
ipConfigResponse = runConsoleCommand(IPCONFIG_COMMAND_WIN);
}
catch (IOException e) {
eprintStackTrace();
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse \n);
while (tokenizerhasMoreTokens()) {
String line = tokenizernextToken()trim();
int macAddressPosition = lineindexOf(:);
if (macAddressPosition <= ) {
continue;
}
String macAddressCandidate = linesubstring(macAddressPosition + )
trim();
if (isMacAddWin(macAddressCandidate)) {
if (realMac == true) {
generateUnique(macAddressCandidate);
}
else {
realMac = true;
}
}
}
return unique;
}
private String runConsoleCommand(String command) throws IOException {
Process p = RuntimegetRuntime()exec(command);
InputStream stdoutStream = new BufferedInputStream(pgetInputStream());
StringBuffer buffer = new StringBuffer();
while (true) {
int c = stdoutStreamread();
if (c == ) {
break;
}
bufferappend( (char) c);
}
String outputText = buffertoString();
stdoutStreamclose();
return outputText;
}
private boolean isMacAddWin(String macAddressCandidate) {
Pattern macPattern = pile([afAF]{}[afAF]{}[afAF]{}[afAF]{}[afAF]{}[afAF]{});
Matcher m = macPatternmatcher(macAddressCandidate);
return mmatches();
}
private boolean isMacAddOSX(String macAddressCandidate) {
if (macAddressCandidatelength() != ) {
return false;
}
else {
return true;
}
}
private void generateUnique(String macAddress) {
if (unique == ) {
unique += macAddress;
}
else {
unique += #;
unique += macAddress;
}
}
public static void main(String [] args) {
Systemoutprintln(NetIDgetMacAddress());
}
}