开发者社区> 问答> 正文

阿里IP地址获取国家和城市?

阿里有没有api或者sdk可以获取IP地址是哪个国家哪个城市的?

展开
收起
游客p3ywff2l5kbpg 2020-10-28 14:19:04 1193 0
1 条回答
写回答
取消 提交回答
  • 下一站是幸福

    1、 阿里的提供的restful api http://ip.taobao.com/service/getIpInfo.php?ip= 需要的ip地址

    1. springmvc下geoip2获取location实现(亲测可行。以下代码直接copy即可用)

    maven的pom.xml里导入geoip2

    com.maxmind.geoip2 geoip2 2.2.0

    下载最新的GeoLite2-City.mmdb和GeoLiteCity.dat

    可以到官网下载或者在我的csdn资源库下载:

    我的csdn资源库:https://download.csdn.net/download/qq_23832313/10501973

    使用实体类保存国家信息

    public class GeoLocation { private String countryCode; private String countryName; private String region; private String regionName; private String city; private String postalCode; private String latitude; private String longitude;

    public String getCountryCode() { return countryCode; }

    public void setCountryCode(String countryCode) { this.countryCode = countryCode; }

    public String getCountryName() { return countryName; }

    public void setCountryName(String countryName) { this.countryName = countryName; }

    public String getRegion() { return region; }

    public void setRegion(String region) { this.region = region; }

    public String getRegionName() { return regionName; }

    public void setRegionName(String regionName) { this.regionName = regionName; }

    public String getCity() { return city; }

    public void setCity(String city) { this.city = city; }

    public String getPostalCode() { return postalCode; }

    public void setPostalCode(String postalCode) { this.postalCode = postalCode; }

    public String getLatitude() { return latitude; }

    public void setLatitude(String latitude) { this.latitude = latitude; }

    public String getLongitude() { return longitude; }

    public void setLongitude(String longitude) { this.longitude = longitude; }

    @Override public String toString() { return "GeoLocation [countryCode=" + countryCode + ", countryName=" + countryName + ", region=" + region + ", regionName=" + regionName + ", city=" + city + ", postalCode=" + postalCode + ", latitude=" + latitude + ", longitude=" + longitude + "]"; }

    service层实现:

    package com.ninesword.nsclick.service;

    import java.io.File; import java.io.IOException; import java.net.InetAddress; import java.net.URL;

    import javax.servlet.http.HttpServletRequest;

    import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component;

    import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.exception.GeoIp2Exception; import com.maxmind.geoip2.model.CityResponse; import com.maxmind.geoip2.record.City; import com.maxmind.geoip2.record.Country; import com.maxmind.geoip2.record.Subdivision; import com.ninesword.nsclick.entity.GeoLocation; import com.ninesword.utils.CommonUtil;

    // Spring Bean的标识. @Component public class GeoLocationService {

    private DatabaseReader reader; private static Logger logger = LoggerFactory.getLogger(GeoLocationService.class);

    public GeoLocationService() { String dataFile = "location/GeoLite2-City.mmdb"; URL url = getClass().getClassLoader().getResource(dataFile);

    if (url == null) { System.err.println("location database is not found - " + dataFile); } else { try { File database = new File(url.getPath()); reader = new DatabaseReader.Builder(database).build(); } catch (Exception e) { e.printStackTrace(); } } }

    /** * 获取ip地址映射的国家 * @param ipAddress * @return */ public GeoLocation getLocationFromRequest(String ip) { GeoLocation location = getLocationV2(ip); return location; }

    /** * 获取ip地址 * @param request * @return */ 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(); } logger.info("Ip from user agent: {}", ip); // 多个反向代理IP时,取第一个 int commaOffset = ip.indexOf(','); if (commaOffset < 0) { ip = ip.equals("0:0:0:0:0:0:0:1") ? "61.183.88.58" : ip; return ip; } ip = ip.substring(0, commaOffset);

    ip = ip.equals("0:0:0:0:0:0:0:1") ? "61.183.88.58" : ip;

    return ip; }

    /** * 获取ip地址映射的国家 * @param ipAddress * @return */ private GeoLocation getLocationV2(String ipAddress) { GeoLocation geoLocation = null; if (null == reader) { //System.err.println("location database is not found."); logger.error("location database is not found."); } else { try { geoLocation = new GeoLocation(); InetAddress ipAdd = InetAddress.getByName(ipAddress); CityResponse response = reader.city(ipAdd);

    Country country = response.getCountry(); geoLocation.setCountryCode(country.getIsoCode()); geoLocation.setCountryName(country.getName());

    Subdivision subdivision = response.getMostSpecificSubdivision(); geoLocation.setRegionName(subdivision.getName());

    City city = response.getCity(); geoLocation.setCity(city.getName()); geoLocation.setPostalCode(response.getPostal().getCode()); geoLocation.setLatitude(String.valueOf(response.getLocation().getLatitude())); geoLocation.setLongitude(String.valueOf(response.getLocation().getLongitude())); } catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage()); } catch (GeoIp2Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } } return geoLocation; } }

    2021-03-14 22:22:40
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
杭州城市数据大脑:数字经济下的新城市基础设施 立即下载
数字政府专场-2019阿里云峰会北京_一云两端 智税惠民_ 立即下载
会员通、服务通、商品通、营销通,全域大数据解决方案有秘诀 立即下载