java获取访问者真实ip和真实地址以及运营商

 

 附上工具类  复制即可用

 

package com.vimi8.bigdata.auth.utils;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 *  根据IP地址获取详细的地域信息
*第一个方法是传入ip获取真实地址
*最后一个方法是获取访问者真实ip 即使通过Nginx多层代理也可以获取
 */
public class AddressUtils {
   
    public static String getAddresses(String content, String encodingString)
            throws UnsupportedEncodingException {

        // 这里调用pconline的接口
        String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
        // 从http://whois.pconline.com.cn取得IP所在的省市区信息
        String returnStr = getResult(urlStr, content, encodingString);
        if (returnStr != null) {
            // 处理返回的省市区信息
            System.out.println(returnStr);
            String[] temp = returnStr.split(",");
            if(temp.length<3){
                return "0";//无效IP,局域网测试
            }
//            String region = (temp[5].split(":"))[1].replaceAll("\"", "");
//            region = decodeUnicode(region);// 省份

            String country = "";
            String area = "";
            String region = "";
            String city = "";
            String county = "";
            String isp = "";
            for(int i=0;i111.121.72.101 中国贵州省贵阳市 电信
//        String ip = "111.121.72.101";
//        String address = "";
//        try {
//            address = addressUtils.getAddresses("ip="+ip, "utf-8");
//        } catch (UnsupportedEncodingException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println(address);
//        // 输出结果为:中国贵州省贵阳市 电信
//    }


    /**
     * 获取访问用户的客户端IP(适用于公网与局域网).
     */
    public static final String getIpAddr(final HttpServletRequest request)
            throws Exception {
        if (request == null) {
            throw (new Exception("getIpAddr method HttpServletRequest Object is null"));
        }
        String ipString = request.getHeader("x-forwarded-for");
        if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString)) {
            ipString = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString)) {
            ipString = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString)) {
            ipString = request.getRemoteAddr();
        }

        // 多个路由时,取第一个非unknown的ip
        final String[] arr = ipString.split(",");
        for (final String str : arr) {
            if (!"unknown".equalsIgnoreCase(str)) {
                ipString = str;
                break;
            }
        }

        return ipString;
    }
}

 

你可能感兴趣的:(java,ip)