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());
}
}