// I get clientIp with below statement.
request.setAttribute("clientIp", clientIp);
// I use below statement to see clientIp on JSP page.
<%=response.encodeURL((String)request.getAttribute("clientIp")) %>
我想知道我是否也可以从clientIP显示国家/地区名称?
赞:10.2.3.4 U.S.A 10.4.5.3英格兰
答案 0 :(得分:2)
如果你想从Java做到这一切,你需要下载一个IP数据库,一个将IP映射到CountryCode的平面文件。
谷歌周围,你会看到有一些免费的,但他们可能不完全是最新的。
它可能看起来像这样:
"16777216","33554431","AU","AUS","AUSTRALIA"
"50331648","69956103","US","USA","UNITED STATES"
"69956104","69956111","BM","BMU","BERMUDA"
"69956112","72349055","US","USA","UNITED STATES"
下载后,您需要将其加载到数据库中,例如SQLLite,以便您可以对其执行查找:
public String getCountryCode(String host) throws Exception {
InetAddress addr = InetAddress.getByName(host);
Long ipNum = ipToInt(addr.getHostAddress());
Statement smt = conn.createStatement();
ResultSet rs = smt.executeQuery("SELECT COUNTRY_CODE3 FROM ip_range WHERE IP_FROM <= "+ipNum+" and IP_TO >= "+ipNum);
if (rs.next())
return rs.getString(1);
return null;
}
另外,有各种web services可供免费使用,限制日常使用,为您付出艰苦的努力,并且可能更新。