获取IP地址

public class IpUtil {

/**获取IP地址**/
  public static String getIp(HttpServletRequest request) {
    String ip = request.getHeader("x-forwarded-for");
    if (isInvalidIp(ip)) {
      ip = request.getHeader("x-real-ip");
      if (isInvalidIp(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
        if (isInvalidIp(ip)) {
          ip = request.getHeader("WL-Proxy-Client-IP");
          if (isInvalidIp(ip)) {
            ip = request.getRemoteAddr();
          }
        }
      }
    }
    return ip;
  }

 
/**检查IP有效性**/
private static boolean isInvalidIp( String ip) { return ip == null || ip. length() == 0 || "unknown". equalsIgnoreCase(ip); } public static void main( String[] args) { HashMap map = getIpInfoBySina( "183.14.134.69"); System. out. println( map. get( "country")); System. out. println( map. get( "province")); System. out. println( map. get( "city")); System. out. println( map); }
/**根据IP地址获取详情**/
public static HashMap getIpInfoBySina( String ip) { String url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + ip; String json = OkHttpUtil. get( url, null); if ( json == null) { return new HashMap(); } return JsonUtil. read( json, HashMap. class); }}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值