一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - JAVA如何獲取客戶端IP地址和MAC地址

JAVA如何獲取客戶端IP地址和MAC地址

2020-12-18 13:18灼眼的健 Java教程

本篇文章主要介紹了JAVA如何獲取客戶端IP地址和MAC地址非常具有實用價值,這里整理了詳細的代碼,需要的朋友可以參考下

本文介紹了JAVA如何獲取客戶端IP地址MAC地址 ,分享給大家,具體如下:

1.獲取客戶端IP地址

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public String getIp(HttpServletRequest request) throws Exception {
  String ip = request.getHeader("X-Forwarded-For");
  if (ip != null) {
    if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
      int index = ip.indexOf(",");
      if (index != -1) {
        return ip.substring(0, index);
      } else {
        return ip;
      }
    }
  }
  ip = request.getHeader("X-Real-IP");
  if (ip != null) {
    if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
      return ip;
    }
  }
  return request.getRemoteAddr();
}

為什么不直接使用request.getRemoteAddr();而要在之前判斷兩個請求頭"X-Forwarded-For"和"X-Real-IP"

X-Forwarded-For: client1, proxy1, proxy2, proxy3

其中的值通過一個 逗號+空格 把多個IP地址區分開, 最左邊(client1)是最原始客戶端的IP地址, 代理服務器每成功收到一個請求,就把請求來源IP地址添加到右邊。

所有我們只取第一個IP地址

X-Real-IP,一般只記錄真實發出請求的客戶端IP

解決用localhost訪問ip為0:0:0:0:0:0:0:1的問題

?
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
public String getIp(HttpServletRequest request) throws Exception {
  String ip = request.getHeader("X-Forwarded-For");
  if (ip != null) {
    if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
      int index = ip.indexOf(",");
      if (index != -1) {
        return ip.substring(0, index);
      } else {
        return ip;
      }
    }
  }
  ip = request.getHeader("X-Real-IP");
  if (ip != null) {
    if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
      return ip;
    }
  }
  ip = request.getHeader("Proxy-Client-IP");
  if (ip != null) {
    if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
      return ip;
    }
  }
  ip = request.getHeader("WL-Proxy-Client-IP");
  if (ip != null) {
    if (!ip.isEmpty() && !"unKnown".equalsIgnoreCase(ip)) {
      return ip;
    }
  }
  ip = request.getRemoteAddr();
  return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
}

2.獲取客戶端MAC地址

?
1
2
UdpGetClientMacAddr umac = new UdpGetClientMacAddr(sip);
String smac = umac.GetRemoteMacAddr();

添加一個獲取MAC的時間限制

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
final UdpGetClientMacAddr umac = new UdpGetClientMacAddr(sip);
//---長時間獲取不到MAC地址則放棄
ExecutorService exec = Executors.newFixedThreadPool(1);
Callable<String> call = new Callable<String>() {
  public String call() throws Exception {
    return umac.GetRemoteMacAddr();
  }
};
try {
  Future<String> future = exec.submit(call);
  String smac = future.get(1000 * 1, TimeUnit.MILLISECONDS);
  loginMonitor.setMacAddress(smac);
} catch (TimeoutException ex) {
  loginMonitor.setMacAddress("獲取失敗");
  logger.info("獲取MAC地址超時");
  ex.printStackTrace();
}
// 關閉線程池
exec.shutdown();
//---

需要先獲取IP地址作為參數構造一個UdpGetClientMacAddr

UdpGetClientMacAddr.java

?
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package shmc.commonsys.security.controller;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
 
/**
 * 主機A向主機B發送“UDP-NetBIOS-NS”詢問包,即向主機B的137端口,發Query包來詢問主機B的NetBIOS Names信息。
 * 其次,主機B接收到“UDP-NetBIOS-NS”詢問包,假設主機B正確安裝了NetBIOS服務........... 而且137端口開放,則主機B會向主機A發送一個“UDP-NetBIOS-NS”應答包,即發Answer包給主機A。
 * 并利用UDP(NetBIOS Name Service)來快速獲取遠程主機MAC地址的方法
 *
 */
public class UdpGetClientMacAddr {
  private String sRemoteAddr;
  private int iRemotePort=137;
  private byte[] buffer = new byte[1024];
  private DatagramSocket ds=null;
 
  public UdpGetClientMacAddr(String strAddr) throws Exception{
    sRemoteAddr = strAddr;
    ds = new DatagramSocket();
  }
 
  public final DatagramPacket send(final byte[] bytes) throws IOException {
    DatagramPacket dp = new DatagramPacket(bytes,bytes.length,InetAddress.getByName(sRemoteAddr),iRemotePort);
    ds.send(dp);
    return dp;
  }
 
  public final DatagramPacket receive() throws Exception {
    DatagramPacket dp = new DatagramPacket(buffer,buffer.length);
    ds.receive(dp);
    return dp;
  }
  public byte[] GetQueryCmd() throws Exception {
    byte[] t_ns = new byte[50];
    t_ns[0] = 0x00;
    t_ns[1] = 0x00;
    t_ns[2] = 0x00;
    t_ns[3] = 0x10;
    t_ns[4] = 0x00;
    t_ns[5] = 0x01;
    t_ns[6] = 0x00;
    t_ns[7] = 0x00;
    t_ns[8] = 0x00;
    t_ns[9] = 0x00;
    t_ns[10] = 0x00;
    t_ns[11] = 0x00;
    t_ns[12] = 0x20;
    t_ns[13] = 0x43;
    t_ns[14] = 0x4B;
 
    for(int i = 15; i < 45; i++){
      t_ns[i] = 0x41;
    }
    t_ns[45] = 0x00;
    t_ns[46] = 0x00;
    t_ns[47] = 0x21;
    t_ns[48] = 0x00;
    t_ns[49] = 0x01;
    return t_ns;
  }
  public final String GetMacAddr(byte[] brevdata) throws Exception {
    // 獲取計算機名
    int i = brevdata[56] * 18 + 56;
    String sAddr="";
    StringBuffer sb = new StringBuffer(17);
    // 先從第56字節位置,讀出Number Of Names(NetBIOS名字的個數,其中每個NetBIOS Names Info部分占18個字節)
    // 然后可計算出“Unit ID”字段的位置=56+Number Of Names×18,最后從該位置起連續讀取6個字節,就是目的主機的MAC地址。
    for(int j = 1; j < 7;j++)
    {
      sAddr = Integer.toHexString(0xFF & brevdata[i+j]);
      if(sAddr.length() < 2)
      {
        sb.append(0);
      }
      sb.append(sAddr.toUpperCase());
      if(j < 6) sb.append(':');
    }
    return sb.toString();
  }
 
  public final void close() throws Exception {
    ds.close();
  }
 
  public final String GetRemoteMacAddr() throws Exception {
    byte[] bqcmd = GetQueryCmd();
    send(bqcmd);
    DatagramPacket dp = receive();
    String smac = GetMacAddr(dp.getData());
    close();
 
    return smac;
  }
   
  public static void main(String args[]) throws Exception{
    UdpGetClientMacAddr umac=new UdpGetClientMacAddr("172.19.1.198");
    umac=new UdpGetClientMacAddr("192.168.16.83");
    System.out.println(umac.GetRemoteMacAddr());
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/huangjian2/p/6238236.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 9999热视频 | 西西人体大胆77777视频 | 国产99在线 | 欧美成人免费草草影院视频 | 亚洲琪琪 | 亚洲黄色高清 | 色悠久久久久综合网小说 | 免费一级生活片 | 午夜爱爱爱爱爽爽爽视频网站 | 国产成人精品一区二区 | 亚洲国产精品日本无码网站 | 天堂在线看 | yy8090韩国日本三理论免费 | 天堂网www在线中文天堂 | 亚洲不卡视频在线观看 | 欧美人与牲动交xxx 欧美人妖另类性hd 欧美人人干 | 国产精品福利在线观看入口 | 精品国产自在在线在线观看 | 亚洲乱码一区二区三区国产精品 | 国产麻豆在线观看网站 | 国产日韩欧美在线播放 | 国语自产自拍秒拍在线视频 | 91麻豆精品国产片在线观看 | 男人女人插 | 国内剧情麻豆 | 成人精品视频一区二区在线 | 色碰视频| 亚洲精品二三区伊人久久 | 欧美在线观看视频一区 | 日本视频免费在线播放 | 好爽好粗 | 天天曰天天干 | 武侠艳妇屈辱的张开双腿 | 2019理论韩国理论中文 | 色欧美在线| 91看片淫黄大片.在线天堂 | 午夜dj免费视频观看社区 | 国产一区精品 | 三叶草私人研究所 | 亚洲天堂999 | 亚洲AV无码乱码在线观看浪潮 |