Java后台获取当前访问客户端的真实IP地址(外网或内网地址)
JSP页面获取客户的IP地址方法
request.getRemoteAddr()
Java后台获取客户的IP地址方法(后台指java/class文件中。再如果域名访问使用了反向代理软件的,上面的方法获取的是内网地址,下面获取的才是客户的外网ip)
public static String getIpAddr(HttpServletRequest request) { 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(); } return ip; }
上面获取的可能有多个ip逗号分隔的情况,即wifi的ip和手机卡运营商的ip,例如:210.22.98.30, 122.190.2.78;如果只想获取一个,此时应该采用下面这种方法
public static String getIpAddr(HttpServletRequest request) { 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(); } if(ip != null) { ip=ip.split(",")[0]; } return ip; }
正在加载评论...
相关文章
- nginx反向代理并隐藏客户端的ip(可解决weblogic受5...
- java,php,js获取ip地址
- java的StringEscapeUtils转义与反转义
- ERROR ParametersInterceptor Developer Notificat...
- maven项目 pom.xml的execution报错的解决方法
- eclipse启动TOMCAT服务器报错8005 8080 8009端口被...
- java发送http的get、post请求
- tomcat启动maven工程报错-Updating status for Tom...
- Mybatis的foreach事列,如何获取foreach的index值
- java中Session缓存和Cache缓存的区别
栏目列表
推荐阅读
- java的StringEscapeUtils转义与反转义
- Mybatis的foreach事列,如何获取foreach的index值
- Java Ehcache缓存的timeToIdleSeconds和timeToLiveSeconds区别
- errcode: 40001, errmsg: invalid credential, access_token is invalid or not latest rid: 5f6981c2-6cc9cb20-6833977a
- 服务器侦听端口 netstat -na 命令详解
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- tomcat启动maven工程报错-Updating status for Tomcat v7.0 Server at localhost...
- Mybatis异常There is no getter for property named 'XXX' in 'class java.lang.String'
- com.googlecode.aviator.AviatorEvaluator规则引擎用法
- java发送http的get、post请求
- StringSubstitutor替换占位符,处理SQL参数
- java拦截器HandlerInterceptor的preHandle、postHandle与afterCompletion三个方法
- java中Session缓存和Cache缓存的区别
- Java8对象集合转List<Integer>id集合,转List<String>name集合,List<Class>对象集合
- Java导出excel利用jxls合并单元格jx:mergeCells实现表格嵌套技术
- java图片文件base64流之间的互转帮助类源码下载
- 利用ObjectMapper,将json数组转List<Map<String,Object>>
- java的DES加密解密辅助类
- Java Ecache缓存工具类源码下载
- IllegalArgumentException: Illegal base64 character 3a