电脑识别不了网卡怎么解决 Java实现读取本机网卡Mac地址
Java实现读取本机网卡Mac地址
本方法主要用来限制系统在其他的机器上运行 其实原理简单的很 没有调用第三方插件 代码如下:
package users util;
import java io *;
import java util *;
import java util regex *;
public class NetID {
String IPCONFIG_MAND_WIN = ipconfig /all ;
boolean realMac = true;
String unique = ;
public static String getMacAddress() {
NetID hwid = new NetID();
return hwid getUnique() trim();
}
private String getUnique() {
String os = System getProperty( os name );
if (os startsWith( Windows )) {
return getUniqueWindows();
}else {
return ;
}
}
private String getUniqueWindows() {
String ipConfigResponse = null;
try {
ipConfigResponse = runConsoleCommand(IPCONFIG_MAND_WIN);
}
catch (IOException e) {
e printStackTrace();
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse n );
while (tokenizer hasMoreTokens()) {
String line = tokenizer nextToken() trim();
int macAddressPosition = line indexOf( : );
if (macAddressPosition <= ) {
continue;
}
String macAddressCandidate = line substring(macAddressPosition + )
trim();
if (isMacAddWin(macAddressCandidate)) {
if (realMac == true) {
generateUnique(macAddressCandidate);
}
else {
realMac = true;
}
}
}
return unique;
}
private String runConsoleCommand(String mand) throws IOException {
Process p = Runtime getRuntime() exec(mand);
InputStream stdoutStream = new BufferedInputStream(p getInputStream());
StringBuffer buffer = new StringBuffer();
while (true) {
int c = stdoutStream read();
if (c == ) {
break;

}
buffer append( (char) c);
}
String outputText = buffer toString();
stdoutStream close();
return outputText;
}
private boolean isMacAddWin(String macAddressCandidate) {
Pattern macPattern = pile( [ a fA F]{ } [ a fA F]{ } [ a fA F]{ } [ a fA F]{ } [ a fA F]{ } [ a fA F]{ } );
Matcher m = macPattern matcher(macAddressCandidate);
return m matches();
}
private boolean isMacAddOSX(String macAddressCandidate) {
if (macAddressCandidate length() != ) {
return false;
}
else {
return true;
}
}
private void generateUnique(String macAddress) {
if (unique == ) {
unique += macAddress;
}
else {
unique += # ;
unique += macAddress;
}
}
public static void main(String [] args) {
System out println(NetID getMacAddress());
}
lishixinzhi/Article/program/Java/hx/201311/25985