❶ java如何獲取mac地址
解釋說明可參考代碼中的注釋即可:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class GetMac {
/**
* java獲取客戶端網卡的MAC地址
*
* @param args
*/
public static void main(String[] args) {
GetMac get = new GetMac();
System.out.println("1="+get.getMAC());
System.out.println("2="+get.getMAC("127.0.0.1"));
}
// 1.獲取客戶端ip地址( 這個必須從客戶端傳到後台):
// jsp頁面下,很簡單,request.getRemoteAddr() ;
// 因為系統的VIew層是用JSF來實現的,因此頁面上沒法直接獲得類似request,在bean里做了個強制轉換
// public String getMyIP() {
// try {
// FacesContext fc = FacesContext.getCurrentInstance();
// HttpServletRequest request = (HttpServletRequest) fc
// .getExternalContext().getRequest();
// return request.getRemoteAddr();
// } catch (Exception e) {
// e.printStackTrace();
// }
// return "";
// }
// 2.獲取客戶端mac地址
// 調用window的命令,在後台Bean里實現 通過ip來獲取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;
}
// 運行速度【慢】
public String getMAC(String ip) {
String str = null;
String macAddress = null;
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; true;) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
macAddress = str
.substring(str.indexOf("MAC Address") + 14);
break;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
return null;
}
return macAddress;
}
}
❷ java怎麼獲取本機的IP地址和MAC
Map<String, String> map = System.getenv();
String userName = map.get("USERNAME");// 獲取用戶名
String computerName = map.get("COMPUTERNAME");// 獲取計算機名
詳細請查看http://blog.csdn.net/zhangxu365/article/details/6883573
String userDomain = map.get("USERDOMAIN");// 獲取計算機域名
❸ java中怎麼獲取電腦的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());
}
}
❹ java socket編程中怎麼獲得本機mac-CSDN論壇
工具/原料
一台配置好java環境的可以上網的電腦
Java獲取本地Mac地址
首先,創建工程,包,和一個類。
在此不加詳述,我們直接看代碼。
這里,我把這個類命名為GetMacAddr
這里,最最關鍵的就是這里這個方法。
我們通過NetworkInterface這個類來操作。
也就是通過getLocalHost()方法先得到本機IP,
然後調用getHardwareAddress()方法得到一個byte數組的地址。
我們把六位地址傳到一個byte數組裡面,然後輸出來就是。
不多廢話,看代碼:
private void getMACAddr()
throws SocketException, UnknownHostException {
// 獲得IP
NetworkInterface netInterface =
NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
// 獲得Mac地址的byte數組
byte[] macAddr = netInterface.getHardwareAddress();
System.out.print("MAC Addr:\t");
// 循環輸出
for (byte b : macAddr) {
// 這里的toHexString()是自己寫的格式化輸出的方法,見下步。
System.out.print(toHexString(b) + " ");
}
}
上一步驟中,為什麼會出現一個toHexString()方法呢?
因為可能10進制轉16進制時候可能會出現單字元,
所以,如果有出現單字元的情況,我們在其前面添加一個「0」做佔位符。
這也是為了視覺的直觀,也夾帶著個人的習慣。
private static String toHexString(int integer) {
// 將得來的int類型數字轉化為十六進制數
String str = Integer.toHexString((int) (integer & 0xff));
// 如果遇到單字元,前置0佔位補滿兩格
if (str.length() == 1) {
str = "0" + str;
}
return str;
}
然後,我們寫一個簡單的main方法測試一下。
public static void main(String[] args)
throws SocketException, UnknownHostException {
new GetMacAddr().getMACAddr();
}
結果無誤,我們得到了本地的MAC地址。
大家一起來試一試吧!
❺ 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"";
}
希望能幫助到你
❻ java怎麼生成本機mac地址
來走一個
publicstaticvoidmain(String[]args)throwsUnknownHostException,SocketException{
InetAddressinetAddress=InetAddress.getLocalHost();
//獲取網卡,獲取地址
byte[]mac=NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
StringBuffersb=newStringBuffer("");
for(inti=0;i<mac.length;i++){
if(i!=0){
sb.append("-");
}
//位元組轉換為整數
inttemp=mac[i]&0xff;
Stringstr=Integer.toHexString(temp);
if(str.length()==1){
sb.append("0"+str);
}else{
sb.append(str);
}
}
System.out.println("本機MAC地址:"+sb.toString().toUpperCase());
}
輸出:
本機MAC地址:B8-88-E3-FA-03-39
❼ java如何不使用HttpServletRequest獲取電腦客戶端ip地址與Mac地址。
import java.net.InetAddress;
import java.net.NetworkInterface;
/**
*@author:
*@version:
*@description:
*/
public class Ipconfig {
public static void main(String[] args) throws Exception {
try {
InetAddress ia=InetAddress.getLocalHost();
String localname=ia.getHostName();
String localip=ia.getHostAddress();
System.out.println("本機名稱是:"+ localname);
System.out.println("本機的ip是 :"+localip);
System.out.println("MAC ......... "+getMACAddress(ia));
} catch (Exception e) {
e.printStackTrace();
}
}
//獲取MAC地址的方法
private static String getMACAddress(InetAddress ia)throws Exception{
//獲得網路介面對象(即網卡),並得到mac地址,mac地址存在於一個byte數組中。
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
//下面代碼是把mac地址拼裝成String
StringBuffer sb = new StringBuffer();
for(int i=0;i<mac.length;i++){
if(i!=0){
sb.append("-");
}
//mac[i] & 0xFF 是為了把byte轉化為正整數
String s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length()==1?0+s:s);
}
//把字元串所有小寫字母改為大寫成為正規的mac地址並返回
return sb.toString().toUpperCase();
}
}
❽ java怎麼獲取系統mac地址
首先,創建工程,包,和一個類。
在此不加詳述,我們直接看代碼。
這里,我把這個類命名為GetMacAddr
這里,最最關鍵的就是這里這個方法。
我們通過NetworkInterface這個類來操作。
也就是通過getLocalHost()方法先得到本機IP,
然後調用getHardwareAddress()方法得到一個byte數組的地址。
我們把六位地址傳到一個byte數組裡面,然後輸出來就是。
不多廢話,看代碼:
private void getMACAddr()
throws SocketException, UnknownHostException {
// 獲得IP
NetworkInterface netInterface =
NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
// 獲得Mac地址的byte數組
byte[] macAddr = netInterface.getHardwareAddress();
System.out.print("MAC Addr:\t");
// 循環輸出
for (byte b : macAddr) {
// 這里的toHexString()是自己寫的格式化輸出的方法,見下步。
System.out.print(toHexString(b) + " ");
}
}
上一步驟中,為什麼會出現一個toHexString()方法呢?
因為可能10進制轉16進制時候可能會出現單字元,
所以,如果有出現單字元的情況,我們在其前面添加一個「0」做佔位符。
這也是為了視覺的直觀,也夾帶著個人的習慣。
private static String toHexString(int integer) {
// 將得來的int類型數字轉化為十六進制數
String str = Integer.toHexString((int) (integer & 0xff));
// 如果遇到單字元,前置0佔位補滿兩格
if (str.length() == 1) {
str = "0" + str;
}
return str;
}
❾ 如何用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地址 。