1. Java web 怎么得到客户端的Mac地址
importjava.net.InetAddress;
importjava.net.NetworkInterface;
importjava.net.SocketException;
importjava.net.UnknownHostException;
/*
*物理地址是48位,别和ipv6搞错了
*/
publicclassLOCALMAC{
/**
*@paramargs
*@throwsUnknownHostException
*@throwsSocketException
*/
publicstaticvoidmain(String[]args)throwsUnknownHostException,SocketException{
//TODOAuto-generatedmethodstub
//得到IP,输出PC-201309011313/122.206.73.83
InetAddressia=InetAddress.getLocalHost();
System.out.println(ia);
getLocalMac(ia);
}
privatestaticvoidgetLocalMac(InetAddressia)throwsSocketException{
//TODOAuto-generatedmethodstub
//获取网卡,获取地址
byte[]mac=NetworkInterface.getByInetAddress(ia).getHardwareAddress();
System.out.println("mac数组长度:"+mac.length);
StringBuffersb=newStringBuffer("");
for(inti=0;i<mac.length;i++){
if(i!=0){
sb.append("-");
}
//字节转换为整数
inttemp=mac[i]&0xff;
Stringstr=Integer.toHexString(temp);
System.out.println("每8位:"+str);
if(str.length()==1){
sb.append("0"+str);
}else{
sb.append(str);
}
}
System.out.println("本机MAC地址:"+sb.toString().toUpperCase());
}
}
2. java如何获取mac地址
以windows举例。
运行命令" cmd ipconfig /all"就会出现以下结果
Physical Address. . . . . . . . . : 20-CF-30-9A-60-EE
。
java就能过这样的命令来获取。以下是示例。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestMac
{
public static void main(String[] args) {
System.out.println("Operation System=" + getOsName());
System.out.println("Mac Address=" + getMACAddress());
System.out.println("通过ip获取mac"+getMACAddress("192.168.1.101"));
}
public static String getOsName() {
String os = "";
os = System.getProperty("os.name");
return os;
}
public static String getMACAddress() {
String address = "";
String os = getOsName();
if (os.startsWith("Windows")) {
try {
String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
}
} else if (os.startsWith("Linux")) {
String command = "/bin/sh -c ifconfig -a";
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("HWaddr") > 0) {
int index = line.indexOf("HWaddr") + "HWaddr".length();
address = line.substring(index);
break;
}
}
br.close();
} catch (IOException e) {
}
}
address = address.trim();
return address;
}
public static String getMACAddress(String ipAddress) {
String str = "", strMAC = "", macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
strMAC = str.substring(str.indexOf("MAC Address") + 14,
str.length());
break;
}
}
}
} catch (IOException ex) {
return "Can't Get MAC Address!";
}
//
if (strMAC.length() < 17) {
return "Error!";
}
macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)
+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)
+ ":" + strMAC.substring(12, 14) + ":"
+ strMAC.substring(15, 17);
//
return macAddress;
}
}
剑天梦的回答原理和我这个一样,都是通过Process 执行命令。 我直接补充到答案里了。不过
我这边运行那个命令出来的结果很多,那么花的时间就长了。优点是能够获取别人的mac地址 。
3. java如何查询本机ip地址和mac地址
//获取mac地址
(){
try{
Enumeration<NetworkInterface>allNetInterfaces=NetworkInterface.getNetworkInterfaces();
byte[]mac=null;
while(allNetInterfaces.hasMoreElements()){
NetworkInterfacenetInterface=(NetworkInterface)allNetInterfaces.nextElement();
if(netInterface.isLoopback()||netInterface.isVirtual()||!netInterface.isUp()){
continue;
}else{
mac=netInterface.getHardwareAddress();
if(mac!=null){
StringBuildersb=newStringBuilder();
for(inti=0;i<mac.length;i++){
sb.append(String.format("%02X%s",mac[i],(i<mac.length-1)?"-":""));
}
if(sb.length()>0){
returnsb.toString();
}
}
}
}
}catch(Exceptione){
_logger.error("MAC地址获取失败",e);
}
return"";
}
//获取ip地址
(){
try{
Enumeration<NetworkInterface>allNetInterfaces=NetworkInterface.getNetworkInterfaces();
InetAddressip=null;
while(allNetInterfaces.hasMoreElements()){
NetworkInterfacenetInterface=(NetworkInterface)allNetInterfaces.nextElement();
if(netInterface.isLoopback()||netInterface.isVirtual()||!netInterface.isUp()){
continue;
}else{
Enumeration<InetAddress>addresses=netInterface.getInetAddresses();
while(addresses.hasMoreElements()){
ip=addresses.nextElement();
if(ip!=null&&ipinstanceofInet4Address){
returnip.getHostAddress();
}
}
}
}
}catch(Exceptione){
_logger.error("IP地址获取失败",e);
}
return"";
}
希望能帮助到你
4. Java代码如何获取客户端的MAC地址
public String getMAC() { String mac = null; try { Process pro = Runtime.getRuntime().exec("cmd.exe /c ipconfig/all"); InputStream is = pro.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String message = br.readLine(); int index = -1; while (message != null) { if ((index = message.indexOf("Physical Address")) > 0) { mac = message.substring(index + 36).trim(); break; } message = br.readLine(); } System.out.println(mac); br.close(); pro.destroy(); } catch (IOException e) { System.out.println("Can't get mac address!"); return null; } return mac; }
5. javaWeb开发,如何取得客户端的MAC,注意,客户端是子网的一个PC
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.net.InetAddress;
importjava.net.NetworkInterface;
/**
*与系统相关的一些常用工具方法.
*
*@authorlvbogun
*@version1.0.0
*/
publicclassSystemTool{
/**
*获取当前操作系统名称.return操作系统名称例如:windowsxp,linux等.
*/
publicstaticStringgetOSName(){
returnSystem.getProperty("os.name").toLowerCase();
}
/**
*获取unix网卡的mac地址.非windows的系统默认调用本方法获取.
*如果有特殊系统请继续扩充新的取mac地址方法.
*
*@returnmac地址
*/
(){
Stringmac=null;
BufferedReaderbufferedReader=null;
Processprocess=null;
try{
//linux下的命令,一般取eth0作为本地主网卡
process=Runtime.getRuntime().exec("ifconfigeth0");
//显示信息中包含有mac地址信息
bufferedReader=newBufferedReader(newInputStreamReader(
process.getInputStream()));
Stringline=null;
intindex=-1;
while((line=bufferedReader.readLine())!=null){
//寻找标示字符串[hwaddr]
index=line.toLowerCase().indexOf("hwaddr");
if(index>=0){//找到了
//取出mac地址并去除2边空格
mac=line.substring(index+"hwaddr".length()+1).trim();
break;
}
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(bufferedReader!=null){
bufferedReader.close();
}
}catch(IOExceptione1){
e1.printStackTrace();
}
bufferedReader=null;
process=null;
}
returnmac;
}
/**
*获取widnows网卡的mac地址.
*
*@returnmac地址
*/
(){
Stringmac=null;
BufferedReaderbufferedReader=null;
Processprocess=null;
try{
//windows下的命令,显示信息中包含有mac地址信息
process=Runtime.getRuntime().exec("ipconfig/all");
bufferedReader=newBufferedReader(newInputStreamReader(
process.getInputStream()));
Stringline=null;
intindex=-1;
while((line=bufferedReader.readLine())!=null){
System.out.println(line);
//寻找标示字符串[physical
index=line.toLowerCase().indexOf("physicaladdress");
if(index>=0){//找到了
index=line.indexOf(":");//寻找":"的位置
if(index>=0){
System.out.println(mac);
//取出mac地址并去除2边空格
mac=line.substring(index+1).trim();
}
break;
}
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(bufferedReader!=null){
bufferedReader.close();
}
}catch(IOExceptione1){
e1.printStackTrace();
}
bufferedReader=null;
process=null;
}
returnmac;
}
/**
*windows7专用获取MAC地址
*
*@return
*@throwsException
*/
()throwsException{
//获取本地IP对象
InetAddressia=InetAddress.getLocalHost();
//获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
byte[]mac=NetworkInterface.getByInetAddress(ia).getHardwareAddress();
//下面代码是把mac地址拼装成String
StringBuffersb=newStringBuffer();
for(inti=0;i<mac.length;i++){
if(i!=0){
sb.append("-");
}
//mac[i]&0xFF是为了把byte转化为正整数
Strings=Integer.toHexString(mac[i]&0xFF);
sb.append(s.length()==1?0+s:s);
}
//把字符串所有小写字母改为大写成为正规的mac地址并返回
returnsb.toString().toUpperCase();
}
}
写一个全局拦截的servlet,只要有请求的时候就调用这个类里面的获取mac地址的方法
Stringos=getOSName();
System.out.println(os);
if(os.equals("windows7")){
Stringmac=getMACAddress();
System.out.println(mac);
}elseif(os.startsWith("windows")){
//本地是windows
Stringmac=getWindowsMACAddress();
System.out.println(mac);
}else{
//本地是非windows系统一般就是unix
Stringmac=getUnixMACAddress();
System.out.println(mac);
}
记得判断一下是什么系统
6. Java web 怎么得到客户端的 Mac 地址
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
/*
* 物理地址是48位,别和ipv6搞错了
*/
public class LOCALMAC {
/**
* @param args
* @throws UnknownHostException
* @throws SocketException
*/
public static void main(String[] args) throws UnknownHostException, SocketException {
// TODO Auto-generated method stub
//得到IP,输出PC-201309011313/122.206.73.83
InetAddress ia = InetAddress.getLocalHost();
System.out.println(ia);
getLocalMac(ia);
}
private static void getLocalMac(InetAddress ia) throws SocketException {
// TODO Auto-generated method stub
//获取网卡,获取地址
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
System.out.println("mac数组长度:"+mac.length);
StringBuffer sb = new StringBuffer("");
for(int i=0; i<mac.length; i++) {
if(i!=0) {
sb.append("-");
}
//字节转换为整数
int temp = mac[i]&0xff;
String str = Integer.toHexString(temp);
System.out.println("每8位:"+str);
if(str.length()==1) {
sb.append("0"+str);
}else {
sb.append(str);
}
}
System.out.println("本机MAC地址:"+sb.toString().toUpperCase());
}
}