Java - 通过IP地址获取用户所在地 (四)

2014-11-24 11:07:16 · 作者: · 浏览: 3
adius
country_confidence
city_confidence
region_confidence
postal_confidence
error
);

my $license_key = 'YOUR_LICENSE_KEY';
my $ip_address = '24.24.24.24';

GetOptions(
'license:s' => \$license_key,
'ip:s' => \$ip_address,
);

my $uri = URI->new('http://geoip.maxmind.com/e');
$uri->query_param( l => $license_key );
$uri->query_param( i => $ip_address );

my $ua = LWP::UserAgent->new( timeout => 5 );
my $response = $ua->get($uri);

die 'Request failed with status ' . $response->code()
unless $response->is_success();

my $csv = Text::CSV_XS->new( { binary => 1 } );
$csv->parse( decode( 'ISO-8859-1', $response->content() ) );

my %omni;
@omni{@fields} = $csv->fields();

binmode STDOUT, ':encoding(UTF-8)';

if ( defined $omni{error} && length $omni{error} ) {
die "MaxMind returned an error code for the request: $omni{error}\n";
}
else {
print "\nMaxMind Omni data for $ip_address\n\n";
for my $field (@fields) {
print sprintf( " %-20s %s\n", $field, $omni{$field} );
}
print "\n";
}

#!/usr/bin/env perl

use strict;
use warnings;

use Encode qw( decode );
use Getopt::Long;
use LWP::UserAgent;
use Text::CSV_XS;
use URI;
use URI::QueryParam;

my @fields = qw(
country_code
country_name
region_code
region_name
city_name
latitude
longitude
metro_code
area_code
time_zone
continent_code
postal_code
isp_name
organization_name
domain
as_number
netspeed
user_type
accuracy_radius
country_confidence
city_confidence
region_confidence
postal_confidence
error
);

my $license_key = 'YOUR_LICENSE_KEY';
my $ip_address = '24.24.24.24';

GetOptions(
'license:s' => \$license_key,
'ip:s' => \$ip_address,
);

my $uri = URI->new('http://geoip.maxmind.com/e');
$uri->query_param( l => $license_key );
$uri->query_param( i => $ip_address );

my $ua = LWP::UserAgent->new( timeout => 5 );
my $response = $ua->get($uri);

die 'Request failed with status ' . $response->code()
unless $response->is_success();

my $csv = Text::CSV_XS->new( { binary => 1 } );
$csv->parse( decode( 'ISO-8859-1', $response->content() ) );

my %omni;
@omni{@fields} = $csv->fields();

binmode STDOUT, ':encoding(UTF-8)';

if ( defined $omni{error} && length $omni{error} ) {
die "MaxMind returned an error code for the request: $omni{error}\n";
}
else {
print "\nMaxMind Omni data for $ip_address\n\n";
for my $field (@fields) {
print sprintf( " %-20s %s\n", $field, $omni{$field} );
}
print "\n";
}
以上都是基于Web Services的,这里我再写个使用本地库的实例,而项目中也使用的是本地库。

首先在项目中需要添加库文件:GeoLiteCity,然后就可以利用这个文件得到城市和国家信息了。项目中如何获取到用户访问的IP地址就不用我说了吧,做过Web开发的人应该都知道。Request里面获取。Java代码:


[java]
public class GeoBusiness {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println(getLocationByIp("220.181.111.147").getCity());
System.out.println(getLocationByIp("220.181.111.147").getCountryName());
}

public static Location getLocationByIp(String ipaddr) throws IOException {
String sep = System.getProperty("file.separator");
String dir = Play.configuration.getProperty("geoip.datdir");
String dbfile = dir + sep + "GeoLiteCity.dat