每次看见QQ上面能够显示对方的ip和地址让人觉得舒服。。刚刚无聊就上网逛了下如何使用java实现获取对方ip和所属地址,找到了些资料,整理成一个工具包供大家使用:
首先是如何获取对方的ip地址,方法很简单,如下:
Java代码
1. public String getIpAddr(HttpServletRequest request) {
2. String ip = request.getHeader("x-forwarded-for");
3. if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
4. ip = request.getHeader("Proxy-Client-IP");
5. }
6. if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
7. ip = request.getHeader("WL-Proxy-Client-IP");
8. }
9. if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
10. ip = request.getRemoteAddr();
11. }
12. return ip;
13. }
public 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是不是很想知道对方在哪里呢,嘿嘿,没问题。使用纯真ip库
下载地址,及使用方法我已经打包上传,里面包括纯真ip库文件,使用该库的工具包,和使用说明。
希望对你有帮助。
1