怎样用JSP获取当前用户的网卡IP和MAC地址
答案:2 悬赏:0 手机版
解决时间 2021-11-26 18:02
- 提问者网友:溺爱和你
- 2021-11-26 04:35
怎样用JSP获取当前用户的网卡IP和MAC地址
最佳答案
- 五星知识达人网友:动情书生
- 2021-11-26 06:04
public String getIpAddr(HttpServletRequest request) throws Exception {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
public String getMACAddress(String ip) throws Exception {
String line = "";
String macAddress = "";
final String MAC_ADDRESS_PREFIX = "MAC Address = ";
final String LOOPBACK_ADDRESS = "127.0.0.1";
//如果为127.0.0.1,则获取本地MAC地址。
if (LOOPBACK_ADDRESS.equals(ip)) {
InetAddress inetAddress = InetAddress.getLocalHost();
//貌似此方法需要JDK1.6。
byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
//下面代码是把mac地址拼装成String
StringBuilder sb = new StringBuilder();
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地址并返回
macAddress = sb.toString().trim().toUpperCase();
return macAddress;
}
//获取非本地IP的MAC地址
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStreamReader isr = new InputStreamReader(p.getInputStream());
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
if (line != null) {
int index = line.indexOf(MAC_ADDRESS_PREFIX);
if (index != -1) {
macAddress = line.substring(index + MAC_ADDRESS_PREFIX.length()).trim().toUpperCase();
}
}
}
br.close();
} catch (IOException e) {
e.printStackTrace(System.out);
}
return macAddress;
}
全部回答
- 1楼网友:十年萤火照君眠
- 2021-11-26 07:17
1, 在JSP页面导入java.net.*
2. 获得IP地址代码
InetAddress hostAndIP = null;
try {
hostAndIP = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
String ip = hostAndIP.toString();
3. 获得mac地址的方法, 接上
byte[] mac = new byte[0];
try {
InetAddress hostAndIP = hostAndIP.getLocalHost();
mac = NetworkInterface.getByInetAddress(hostAndIP).getHardwareAddress();
} catch (SocketException e) {
} catch (UnknownHostException e) {
}
StringBuffer buffer = new StringBuffer("");
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
buffer.append("-");
}
//字节转换为整数
int temp = mac[i] & 0xff;
String str = Integer.toHexString(temp);
if (str.length() == 1) {
buffer.append("0" + str);
} else {
buffer.append(str);
}
}
String mac= buffer.toString();
这种是直接把代码写在页面上,建议封装一个工具类直接在页面上导入后调用方法获得IP和MAC,或者写成自定义标签
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯