JSF 获取客户端的IP地址
来源:互联网 发布:影视特效制作软件 编辑:程序博客网 时间:2024/06/05 08:44
在jsf 中直接使用request.getRemoteAddr() 来获取客户端的IP 会报空指针异常。所以在后台bean中写了个方法
public String getClientIP() {
try {
FacesContext fc = FacesContext.getCurrentInstance();
//实例化request
HttpServletRequest request = (HttpServletRequest)fc.getExternalContext().getRequest();
return request.getRemoteAddr();
}
catch (Exception e) {
e.printStackTrace();
}
return "";
}
这样就可以获取没有使用代理的客户端的IP地址,若使用了代理则获得的是127.0.0.1或者是192.168.1.110而不是真实的IP地址
可以使用以下方法获得
public String getIpAddr(HttpServletRequest request) {
try{
FacesContext fc = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)fc.getExternalContext().getRequest();
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.getRemoteAddr();
}
} catch (Exception e) {
e.printStackTrace();
}
return ip;
}
也可以获得客户端的MAC地址
public String getMACAddress(String ip){
String str = "";
String macAddress = "";
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStreamReader ir = new InputStreamReader(p.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) {
macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
break;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
return macAddress;
}
- JSF 获取客户端的IP地址
- JSF/JAVA 根据IP获取客户端Mac地址
- 获取客户端的IP地址
- 获取客户端的ip地址
- 获取客户端的IP地址
- 获取客户端的IP地址
- 获取客户端的IP地址
- 获取客户端的IP地址
- 获取客户端的IP地址
- 获取客户端的ip地址
- 获取客户端的IP地址
- 获取客户端IP地址
- 获取客户端ip地址
- 获取客户端IP地址
- 客户端获取IP地址
- 获取客户端IP地址
- 获取客户端IP地址
- 获取客户端IP地址
- linux下32位机与64位机基本数据类型长度对比
- 大型系统的框架实现规划
- weblogic 下载网址
- 过渡到SSAS之一:简单模型认识
- 水晶报表
- JSF 获取客户端的IP地址
- mysql 导出数据
- 如何在Java中获取当前代码行行号和文件名(__FILE__, __LINE__)
- 用CMD查看进程
- Linux 扫描之ILO
- 第一次登陆,希望能在这个网站认识很多同道中人
- [转]如何系统的学习linux
- 我的最爱-电影
- 贝叶斯算法(bayesian)介绍