获取服务器真实ip

前置条件

 无

作者本人环境配置

 1.

getHeader方法基本用法


                System.out.println("rotocol: " + request.getProtocol());   

System.out.println("Scheme: " + request.getScheme());   

System.out.println("Server Name: " + request.getServerName() );   

System.out.println("Server Port: " + request.getServerPort());   

System.out.println("rotocol: " + request.getProtocol());   

System.out.println("Server Info: " + getServletConfig().getServletContext().getServerInfo());   

System.out.println("Remote Addr: " + request.getRemoteAddr());   

System.out.println("Remote Host: " + request.getRemoteHost());   

System.out.println("Character Encoding: " + request.getCharacterEncoding());   

System.out.println("Content Length: " + request.getContentLength());   

System.out.println("Content Type: "+ request.getContentType());   

System.out.println("Auth Type: " + request.getAuthType());   

System.out.println("HTTP Method: " + request.getMethod());   

System.out.println("ath Info: " + request.getPathInfo());   

System.out.println("ath Trans: " + request.getPathTranslated());   

System.out.println("Query String: " + request.getQueryString());   

System.out.println("Remote User: " + request.getRemoteUser());   

System.out.println("Session Id: " + request.getRequestedSessionId());   

System.out.println("Request URI: " + request.getRequestURI());   

System.out.println("Servlet Path: " + request.getServletPath());   

System.out.println("Accept: " + request.getHeader("Accept"));   

System.out.println("Host: " + request.getHeader("Host"));   

System.out.println("Referer : " + request.getHeader("Referer"));   

System.out.println("Accept-Language : " + request.getHeader("Accept-Language"));   

System.out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding"));   

System.out.println("User-Agent : " + request.getHeader("User-Agent"));   

System.out.println("Connection : " + request.getHeader("Connection"));   

System.out.println("Cookie : " + request.getHeader("Cookie"));   

System.out.println("Created : " + session.getCreationTime());   

获取真实ip

    JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的。但是在通过了 Apache,Nagix等反向代理软件就不能获取到客户端的真实IP地址了。如果使用了反向代理软件,用 request.getRemoteAddr()方法获取的IP地址是:127.0.0.1或 192.168.1.110,而并不是客户端的真实IP。

      经过代理以后,由于在客户端和服务之间增加了中间层,因此服务器无法直接拿到客户端的 IP,服务器端应用也无法直接通过转发请求的地址返回给客户端。但是在转发请求的HTTP头信息中,增加了X-FORWARDED-FOR信息。用以跟踪原有的客户端 IP地址和原来客户端请求的服务器地址。

   下面是获取真实ip的一种方法,可以参考

               /**

 * 获取客户端ip

 * @return

 */

protected String getIp() {

HttpServletRequest request = this.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();

}

return ip.trim().equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;

}

其他总结

      乱码问题,如果request.getHeader("Referer")链接中带有中文参数,那么在经过跳转的时候,就会出现乱码,这个时候需要用到String keyWords = new String(str.getBytes("ISO-8859-1"),"GBK");来进行编码转换!可是如果不是通过request.getHeader("Referer")链接链过来的也用keyWords = new String(str.getBytes("ISO-8859-1"),"GBK");来进行编码转换的话,就会出现乱码,解决的办法,首先,我用str.matches( "[\\u4E00-\\u9FA5]+")来判断这个字符串中是否有中文,如果没有中文,则为乱码或者数字或者英文或者其他符号,那么我们则进行编码转换,如果是中文,则不进行编码转换

上一篇:ImportError: No module named osgeo(没有osgeo的模块)


下一篇:中国计算机学会 × 高德地图 发布“POI名称生成”赛题,诚邀全球英才组队参加