java 如何在java中根据IP地址获取城市名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2257170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:19:59  来源:igfitidea点击:

How to get city name based on IP address in java?

javaip-addresszipcodeip-geolocationcity

提问by Abhishek Dhote

Is it possible to know the city name based on IP address in java?

是否可以根据java中的IP地址知道城市名称?

回答by Umair Jabbar

are you using jsp or what ? for city name over client side you can use google's API which will return you the geographical details of the user mapped with the IP. As google's documentation say you can get the following

你用的是jsp还是什么?对于客户端上的城市名称,您可以使用谷歌的 API,它将返回与 IP 映射的用户的地理详细信息。正如谷歌的文档所说,您可以获得以下信息

When populated, the google.loader.ClientLocation object is populated with the following metro-level granularity properties:

填充后, google.loader.ClientLocation 对象将填充以下地铁级粒度属性:

* ClientLocation.latitude — supplies the low resolution latitude associated with the client's IP address
* ClientLocation.longitude — supplies the low resolution longitude associated with the client's IP address
* ClientLocation.address.city — supplies the name of the city associated with the client's IP address
* ClientLocation.address.country — supplies the name of the country associated with the client's IP address
* ClientLocation.address.country_code — supplies the name of the ISO 3166-1 country code associated with the client's IP address
* ClientLocation.address.region — supplies the country specific region name associated with the client's IP address

for further reference see the complete documentation at http://code.google.com/apis/ajax/documentation/

如需进一步参考,请参阅http://code.google.com/apis/ajax/documentation/ 上的完整文档

回答by Guido

You have to use a service such as hostip.infothat maps an IP to its physical location, and offer an APIto developers to simplify the task.

您必须使用诸如hostip.info 之类的服务将 IP 映射到其物理位置,并向开发人员提供API以简化任务。

There are many other services, paid and for free, that you can find at Google.

您可以在Google 上找到许多其他付费和免费服务。

回答by Cowan

If an online API as the others have suggested won't suit your application, I have previously successfully used MaxMind GeoLite City, which comes with a Java API (among many others). Just download the database files, point your API code at them, and off you go. Simple and straightforward.

如果其他人建议的在线 API 不适合您的应用程序,我之前已经成功使用了MaxMind GeoLite City,它带有 Java API(以及许多其他 API)。只需下载数据库文件,将您的 API 代码指向它们,然后就可以了。简单明了。