JAVA如何获取客户端IP地址和MAC地址

JAVA如何获取客户端IP地址和MAC地址,第1张

/

获取IP地址

@return

/

public static String GetNetIp() {

URL infoUrl = null;

InputStream inStream = null;

String line = "";

try {

infoUrl = new URL(">

通过设备开通WiFi连接获取Mac地址是最可取的,代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/

设备开通WiFi连接,通过wifiManager获取Mac地址

@author 高焕杰

/

public static String getMacFromWifi(Context context){

ConnectivityManager connectivityManager = (ConnectivityManager) contextgetSystemService(ContextCONNECTIVITY_SERVICE);

State wifiState = connectivityManagergetNetworkInfo(ConnectivityManagerTYPE_WIFI)getState();

if(wifiState == NetworkInfoStateCONNECTED){//判断当前是否使用wifi连接

WifiManager wifiManager = (WifiManager) contextgetSystemService(ContextWIFI_SERVICE);

if (!wifiManagerisWifiEnabled()) { //如果当前wifi不可用

wifiManagersetWifiEnabled(true);

}

WifiInfo wifiInfo = wifiManagergetConnectionInfo();

return wifiInfogetMacAddress();

}

return null;

}

除了上面这种方法,网上还给出了另外两种方法:

1、通过调用Linux的busybox命令获取Mac地址:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/

通过调用Linux的busybox命令获取Mac地址

@author 高焕杰

/

private static String getMacFromCallCmd(){

try {

String readLine = ;

Process process = RuntimegetRuntime()exec(busybox ifconfig);

BufferedReader bufferedReader = new BufferedReader (new InputStreamReader(processgetInputStream()));

while ((readLine = bufferedReaderreadLine ()) != null) {//执行命令cmd,只取结果中含有HWaddr的这一行

if(readLinecontains(HWaddr)){

return readLinesubstring(readLineindexOf(HWaddr)+6, readLinelength()-1);

}

}

}catch(Exception e) { //如果因设备不支持busybox工具而发生异常。

eprintStackTrace();

}

return null;

}

注意:这种方法在Android Pad中可以准确获取到的Mac地址,但是在Android手机中无法准确获取到。

2、通过查询记录了MAC地址的文件(文件路径:“/proc/net/arp”)获取Mac地址:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

/

通过查询记录了MAC地址的文件(文件路径:“/proc/net/arp”)获取Mac地址

@author 高焕杰

/

private static String getMacFromFile(Context context){

String readLine =;

BufferedReader bufferedReader = null;

try {

bufferedReader = new BufferedReader(new FileReader(new File(/proc/net/arp)));

int rollIndex = 0;

while((readLine = bufferedReaderreadLine())!=null){

if(rollIndex == 1){

break;

}

rollIndex = rollIndex + 1;

}

} catch (IOException e) {

eprintStackTrace();

} finally {

if (bufferedReader != null) {

try {

bufferedReaderclose();

} catch (IOException e) {

eprintStackTrace();

}

}

}

if(readLine !=null && readLinelength()>1){

String[] subReadLineArray = readLinesplit( );

int rollIndex = 1;

for(int i = 0; i < subReadLineArraylength; ++i){

if(!TextUtilsisEmpty(subReadLineArray[i])){

if(rollIndex == 4){

return subReadLineArray[i];

}

rollIndex = rollIndex + 1;

}

}

}

return null;

}

注意:无论在Android Pad中还是在Android手机中,这种方法都无法准确获取到Mac地址。

package tmp;

import javanetNetworkInterface;

import javautilEnumeration;

public class MacUtil {

public static String getMacAddress() throws Exception {

String s="";

Enumeration<NetworkInterface> ni = NetworkInterface

getNetworkInterfaces();

while (nihasMoreElements()) {

NetworkInterface netI = ninextElement();

byte[] bytes = netIgetHardwareAddress();

if (netI != null && bytes != null && byteslength == 6) {

StringBuffer sb = new StringBuffer();

for (byte b : bytes) {

// 与11110000作按位与运算以便读取当前字节高4位

sbappend(IntegertoHexString((b & 240) >> 4));

// 与00001111作按位与运算以便读取当前字节低4位

sbappend(IntegertoHexString(b & 15));

sbappend("-");

}

sbdeleteCharAt(sblength() - 1);

s+=sbtoString()toUpperCase()+"\n";

}

}

return s;

}

public static void main(String[] args) throws Exception {

Systemoutprintln(MacUtilgetMacAddress());

}

}

ipconfig是Windows下命令提示符支持的一个命令,可以查询到你的机器的ip等网络配置

RuntimegetRuntime()exec("ipconfig /all"); 就是执行该命令

if (lineindexOf("Physical Address") > 0)表示如果在line中查找到Physical Address,就继续执行if中的语句,否则如果找不到,lineindexOf("Physical Address")的返回值=-1

以上就是关于JAVA如何获取客户端IP地址和MAC地址全部的内容,包括:JAVA如何获取客户端IP地址和MAC地址、使用java程序怎么从win7下得到mac地址、如何使用Java代码获取Android和ios移动终端Mac地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9339467.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存