求代码,怎么用JAVA取机器的mac地址,立即揭贴。 求代码,怎么取机器的mac地址,立即揭贴。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果你是做J2EE取远程的话,用java办不了,建议用javaScript来完成。如果取本地的话,java可以调用本机windows的api来完成,网上有很多这种例子 ipconfig -all 自己分析一下代码。。具体的代码你自己去写吧。。 JAVA取MAC地址,只能JNI了。 import java.net.InetAddress;import java.io.InputStream;import java.io.BufferedInputStream;import java.io.IOException;import java.text.ParseException;import java.util.StringTokenizer;public final class NetworkInfo {private final static String getMacAddress() throws IOException {String os = System.getProperty("os.name");try {if (os.startsWith("Windows")) {return windowsParseMacAddress(windowsRunIpConfigCommand());} else if (os.startsWith("Linux")) {return linuxParseMacAddress(linuxRunIfConfigCommand());} else {throw new IOException("unknown operating system: " + os);}} catch (ParseException ex) {ex.printStackTrace();throw new IOException(ex.getMessage());}}/** Linux stuff*/private final static String linuxParseMacAddress(String ipConfigResponse)throws ParseException {String localHost = null;try {localHost = InetAddress.getLocalHost().getHostAddress();} catch (java.net.UnknownHostException ex) {ex.printStackTrace();throw new ParseException(ex.getMessage(), 0);}StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");String lastMacAddress = null;while (tokenizer.hasMoreTokens()) {String line = tokenizer.nextToken().trim();boolean containsLocalHost = line.indexOf(localHost) >= 0;// see if line contains IP addressif (containsLocalHost && lastMacAddress != null) {return lastMacAddress;}// see if line contains MAC addressint macAddressPosition = line.indexOf("HWaddr");if (macAddressPosition <= 0)continue;String macAddressCandidate = line.substring(macAddressPosition + 6).trim();if (linuxIsMacAddress(macAddressCandidate)) {lastMacAddress = macAddressCandidate;continue;}}ParseException ex = new ParseException("cannot read MAC address for "+ localHost + " from [" + ipConfigResponse + "]", 0);ex.printStackTrace();throw ex;}private final static boolean linuxIsMacAddress(String macAddressCandidate) {// TODO: use a smart regular expressionif (macAddressCandidate.length() != 17)return false;return true;}private final static String linuxRunIfConfigCommand() throws IOException {Process p = Runtime.getRuntime().exec("ifconfig");InputStream stdoutStream = new BufferedInputStream(p.getInputStream());StringBuffer buffer = new StringBuffer();for (;;) {int c = stdoutStream.read();if (c == -1)break;buffer.append((char) c);}String outputText = buffer.toString();stdoutStream.close();return outputText;}/** Windows stuff*/private final static String windowsParseMacAddress(String ipConfigResponse)throws ParseException {String localHost = null;try {localHost = InetAddress.getLocalHost().getHostAddress();} catch (java.net.UnknownHostException ex) {ex.printStackTrace();throw new ParseException(ex.getMessage(), 0);}StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");String lastMacAddress = null;while (tokenizer.hasMoreTokens()) {String line = tokenizer.nextToken().trim();// see if line contains IP addressif (line.endsWith(localHost) && lastMacAddress != null) {return lastMacAddress;}// see if line contains MAC addressint macAddressPosition = line.indexOf(":");if (macAddressPosition <= 0)continue;String macAddressCandidate = line.substring(macAddressPosition + 1).trim();if (windowsIsMacAddress(macAddressCandidate)) {lastMacAddress = macAddressCandidate;continue;}}ParseException ex = new ParseException("cannot read MAC address from ["+ ipConfigResponse + "]", 0);ex.printStackTrace();throw ex;}private final static boolean windowsIsMacAddress(String macAddressCandidate) {// TODO: use a smart regular expressionif (macAddressCandidate.length() != 17)return false;return true;}private final static String windowsRunIpConfigCommand() throws IOException {Process p = Runtime.getRuntime().exec("ipconfig /all");InputStream stdoutStream = new BufferedInputStream(p.getInputStream());StringBuffer buffer = new StringBuffer();for (;;) {int c = stdoutStream.read();if (c == -1)break;buffer.append((char) c);}String outputText = buffer.toString();stdoutStream.close();return outputText;}/** Main*/public final static void main(String[] args) {try {System.out.println("Network infos");System.out.println(" Operating System: "+ System.getProperty("os.name"));System.out.println(" IP/Localhost: "+ InetAddress.getLocalHost().getHostAddress());System.out.println(" MAC Address: " + getMacAddress());} catch (Throwable t) {t.printStackTrace();}}} 有一个扩展包jcap,里面有相关的类方法可以获得我曾用JAVA做过一个嗅探器用到过 http://forum.java.sun.com/thread.jspa?forumID=4&threadID=245711 java nettbeans 编程问题,输入时以空格区分两个数字结果不正确,以enter则结果正确。困惑中啊,另外有什么写得不好的地方也请指出,谢谢啊 有点小疑惑,帮忙看一下 Jsmooth打包问题 新手提问:如何判断一个类被实例化了? 奇怪的例子,Java中switch() 中可以用字符串来进行判断吗? 怎样才能中止一个Thread对象并将其及时释放? 关于菜单 小菜问题!~~ 关于我学JAVA的一些初步问题? 快速排序有问题,求修改 如何利用random()生成零到一百的随机整数 一个小问题 请指点 谢谢
具体的代码你自己去写吧。。
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.StringTokenizer;public final class NetworkInfo {
private final static String getMacAddress() throws IOException {
String os = System.getProperty("os.name");try {
if (os.startsWith("Windows")) {
return windowsParseMacAddress(windowsRunIpConfigCommand());
} else if (os.startsWith("Linux")) {
return linuxParseMacAddress(linuxRunIfConfigCommand());
} else {
throw new IOException("unknown operating system: " + os);
}
} catch (ParseException ex) {
ex.printStackTrace();
throw new IOException(ex.getMessage());
}
}/*
* Linux stuff
*/
private final static String linuxParseMacAddress(String ipConfigResponse)
throws ParseException {
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch (java.net.UnknownHostException ex) {
ex.printStackTrace();
throw new ParseException(ex.getMessage(), 0);
}StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
String lastMacAddress = null;while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
boolean containsLocalHost = line.indexOf(localHost) >= 0;// see if line contains IP address
if (containsLocalHost && lastMacAddress != null) {
return lastMacAddress;
}// see if line contains MAC address
int macAddressPosition = line.indexOf("HWaddr");
if (macAddressPosition <= 0)
continue;String macAddressCandidate = line.substring(macAddressPosition + 6)
.trim();
if (linuxIsMacAddress(macAddressCandidate)) {
lastMacAddress = macAddressCandidate;
continue;
}
}ParseException ex = new ParseException("cannot read MAC address for "
+ localHost + " from [" + ipConfigResponse + "]", 0);
ex.printStackTrace();
throw ex;
}private final static boolean linuxIsMacAddress(String macAddressCandidate) {
// TODO: use a smart regular expression
if (macAddressCandidate.length() != 17)
return false;
return true;
}private final static String linuxRunIfConfigCommand() throws IOException {
Process p = Runtime.getRuntime().exec("ifconfig");
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());StringBuffer buffer = new StringBuffer();
for (;;) {
int c = stdoutStream.read();
if (c == -1)
break;
buffer.append((char) c);
}
String outputText = buffer.toString();stdoutStream.close();return outputText;
}/*
* Windows stuff
*/
private final static String windowsParseMacAddress(String ipConfigResponse)
throws ParseException {
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch (java.net.UnknownHostException ex) {
ex.printStackTrace();
throw new ParseException(ex.getMessage(), 0);
}StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
String lastMacAddress = null;while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();// see if line contains IP address
if (line.endsWith(localHost) && lastMacAddress != null) {
return lastMacAddress;
}// see if line contains MAC address
int macAddressPosition = line.indexOf(":");
if (macAddressPosition <= 0)
continue;String macAddressCandidate = line.substring(macAddressPosition + 1)
.trim();
if (windowsIsMacAddress(macAddressCandidate)) {
lastMacAddress = macAddressCandidate;
continue;
}
}ParseException ex = new ParseException("cannot read MAC address from ["
+ ipConfigResponse + "]", 0);
ex.printStackTrace();
throw ex;
}private final static boolean windowsIsMacAddress(String macAddressCandidate) {
// TODO: use a smart regular expression
if (macAddressCandidate.length() != 17)
return false;return true;
}private final static String windowsRunIpConfigCommand() throws IOException {
Process p = Runtime.getRuntime().exec("ipconfig /all");
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());StringBuffer buffer = new StringBuffer();
for (;;) {
int c = stdoutStream.read();
if (c == -1)
break;
buffer.append((char) c);
}
String outputText = buffer.toString();stdoutStream.close();return outputText;
}/*
* Main
*/
public final static void main(String[] args) {
try {
System.out.println("Network infos");System.out.println(" Operating System: "
+ System.getProperty("os.name"));
System.out.println(" IP/Localhost: "
+ InetAddress.getLocalHost().getHostAddress());
System.out.println(" MAC Address: " + getMacAddress());
} catch (Throwable t) {
t.printStackTrace();
}
}
}
我曾用JAVA做过一个嗅探器用到过