我如何在Java中使用IP地址找到城市名称

发布于 2024-08-25 05:30:22 字数 1436 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪荡不羁 2024-09-01 05:30:22

来自 Andrey 链接,这里是如何构建查询,此代码将返回一个 HTML 文件,其中包含当前 IP 的所有详细信息,包括城市;

String IP= "123.123.123.123";
URL link = new URL("http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress="+IP);

BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
String inputLine;

while ((inputLine = in.readLine()) != null){
     System.out.println(inputLine);             
}

in.close();

更新,2013 年 5 月 23 日

之前的答案是好的,但它不是 API 调用,它读取我之前提供的 HTML 页面,因为我没有找到任何免费的 API。接下来是一个 REST API 调用,可以轻松使用,并将返回所需的所有信息,建议使用这个:

String ip = "2.51.255.200"; 
URL url = new URL("http://freegeoip.net/csv/" + ip);
connection = (HttpURLConnection) url.openConnection();
connection.connect();

InputStream is = connection.getInputStream();

int status = connection.getResponseCode();
if (status != 200) {
    return null;
}

reader = new BufferedReader(new InputStreamReader(is));
for (String line; (line = reader.readLine()) != null;) {
    //this API call will return something like:
    "2.51.255.200","AE","United Arab Emirates","03","Dubai","Dubai","","x-coord","y-coord","",""
    // you can extract whatever you want from it
}

From Andrey link, here is how to construct the inquiry, this code will return an HTML file with all details of current IP, including city;

String IP= "123.123.123.123";
URL link = new URL("http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress="+IP);

BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
String inputLine;

while ((inputLine = in.readLine()) != null){
     System.out.println(inputLine);             
}

in.close();

UPDATE, 23 May 2013

The previous answer is ok, but it's not an API call, it reads an HTML page, that I provided previously because I didn't find any free APIs. Next is a REST API call that can be used easily and will return all the info required, it's recommended to use this one:

String ip = "2.51.255.200"; 
URL url = new URL("http://freegeoip.net/csv/" + ip);
connection = (HttpURLConnection) url.openConnection();
connection.connect();

InputStream is = connection.getInputStream();

int status = connection.getResponseCode();
if (status != 200) {
    return null;
}

reader = new BufferedReader(new InputStreamReader(is));
for (String line; (line = reader.readLine()) != null;) {
    //this API call will return something like:
    "2.51.255.200","AE","United Arab Emirates","03","Dubai","Dubai","","x-coord","y-coord","",""
    // you can extract whatever you want from it
}
長街聽風 2024-09-01 05:30:22

如果您的应用程序部署在防火墙后面。因此,我们可以使用 GeoLite,而不是调用 API,下面是示例代码。

http://geolite.maxmind.com/ 下载 City.dat 文件下载/geoip/database/GeoLiteCity.dat.gz

            File    datapath = new File("GeoLiteCity.dat");
            LookupService cl = new LookupService(datapath,
                    LookupService.GEOIP_MEMORY_CACHE
                            | LookupService.GEOIP_CHECK_CACHE);
            String cityName = cl.getLocation(ipAddress).city;

If your Application is deployed behind the firewall. So instead of calling a API, we can use GeoLite below is the sample code.

Download City.dat file from http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

            File    datapath = new File("GeoLiteCity.dat");
            LookupService cl = new LookupService(datapath,
                    LookupService.GEOIP_MEMORY_CACHE
                            | LookupService.GEOIP_CHECK_CACHE);
            String cityName = cl.getLocation(ipAddress).city;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文