码迷,mamicode.com
首页 > 其他好文 > 详细

浏览器获取ip地址

时间:2015-01-28 19:26:50      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:

/**
* 获取浏览器的ip地址
* @param request
* @return
*/
public static String getIP(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (!checkIP(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (!checkIP(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (!checkIP(ip)) {
ip = request.getRemoteAddr();
}
if("0:0:0:0:0:0:0:1".equals(ip)){
ip = "127.0.0.1";
}
return ip;
}

private static boolean checkIP(String ip) {
if (ip == null || ip.length() == 0 || "unkown".equalsIgnoreCase(ip)
|| ip.split("\\.").length != 4) {
return false;
}
return true;
}

 

/**
* 获取浏览器类型
* @param request
* @return
*/
public static String getUserAgent(HttpServletRequest request) {
String agent = request.getHeader("User-Agent");
return agent;
}

浏览器获取ip地址

标签:

原文地址:http://www.cnblogs.com/1246447850qqcom/p/4256406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!