JSP获取网络IP地址
import javax.servlet.http.HttpServletRequest; public class RemoteAddress { public static String getRealClientIP(HttpServletRequest request) { String ip = request.getHeader("X-Forwarded-For"); System.out.println(ip); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); System.out.println(ip); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); System.out.println(ip); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); System.out.println(ip); } return ip; } }
之前用如上代码测试都发现结果是127.0.0.1,用本机做服务器挂网上中转也是。以为是代码出现问题,后来直接挂到空间上,发现正常显示出外网了,于是用移动网访问本机服务器也可以正常显示IP了而不再是127.0.0.1或者::1 的IPv6的地址。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。