java 判断是同一台机器的请求 代码列子
时间: 2023-11-06 17:06:03 浏览: 69
首先,需要获取当前机器的IP地址:
```
InetAddress addr = InetAddress.getLocalHost();
String ip = addr.getHostAddress().toString();
```
然后,可以获取当前机器的MAC地址:
```
NetworkInterface network = NetworkInterface.getByInetAddress(addr);
byte[] mac = network.getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
String macAddress = sb.toString();
```
最后,可以将IP地址和MAC地址合并生成一个唯一标识,用于判断是否同一台机器的请求:
```
String uniqueId = ip + ";" + macAddress;
```
阅读全文