简体   繁体   English

通过 IP 获取地理位置(Spigot 1.8 和 1.13.2)

[英]Get Geolocation by IP (Spigot 1.8 & 1.13.2)

i Want to find out the geolocation by only providing the ip adress.我想通过只提供 ip 地址来找出地理位置。 My Aim is to save city, country, postal code and other informations.我的目标是保存城市、国家、邮政编码和其他信息。

CraftPlayer cp = (CraftPlayer)p;
String adress = cp.getAddress();

Any short possibilities, to find out by only using ip?任何简短的可能性,仅使用 ip 即可找到?

There are a lot of websites that provide free databases for IP geolocation.有很多网站提供用于 IP 地理定位的免费数据库。

Examples include:例子包括:

At the plugin startup you could download one of these databases and then query it locally during runtime.在插件启动时,您可以下载这些数据库之一,然后在运行时在本地查询它。

If you choose do download the .bin format you will have to initialize a local database and then import the data.如果选择下载 .bin 格式,则必须初始化本地数据库,然后导入数据。 Otherwise you could just use the csv file with a Java library like opencsv .否则,您可以将 csv 文件与像opencsv这样的 Java 库一起使用

From the documentation of opencsv:从 opencsv 的文档中:

For reading, create a bean to harbor the information you want to read, annotate the bean fields with the opencsv annotations, then do this:对于读取,创建一个 bean 来容纳您要读取的信息,使用 opencsv 批注对 bean 字段进行批注,然后执行以下操作:

 List<MyBean> beans = new CsvToBeanBuilder(FileReader("yourfile.csv")) .withType(Visitors.class).build().parse();

Link to documentation: http://opencsv.sourceforge.net文档链接: http : //opencsv.sourceforge.net

I recommend using http://ip-api.com/docs/api:newline_separated我建议使用http://ip-api.com/docs/api:newline_separated

You can then chose what information you need and create your HTTP-link like:然后,您可以选择您需要的信息并创建您的 HTTP 链接,例如:

http://ip-api.com/line/8.8.8.8?fields=49471 http://ip-api.com/line/8.8.8.8?fields=49471

The result in this example would be:此示例中的结果将是:

success
United States
US
VA
Virginia
Ashburn
20149
America/New_York

So you can create a method in Java to read HTTP and split it at \\n to get the lines:因此,您可以在 Java 中创建一个方法来读取 HTTP 并将其拆分为\\n以获取以下行:

private void whatever(String ip) {
    String ipinfo = getHttp("http://ip-api.com/line/" + ip + "?fields=49471");
    if (ipinfo == null || !ipinfo.startsWith("success")) {
        // TODO: failed
        return;
    }
    String[] lines = ipinfo.split("\n");
    // TODO: now you can get the info
    String country = lines[1];
    /*
    ...
     */
}

private static String getHttp(String url) {
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line).append(System.lineSeparator());
        }
        br.close();
        return sb.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

just make sure not to create to many querys in a short amount of time since ip-api.com will ban you for it.只要确保不要在短时间内创建多个查询,因为 ip-api.com 会因此禁止您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM