IPアドレスを特定の地理的位置に解析する
16768 ワード
宝を洗って提供する住所の解析庫を使ってIPの解析を行って、具体的なコードは以下の通りです:
ここで使用されるfastjson jarは、maven依存性が次のようになります.
アドレス解析ツールクラスをテストします.
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* IP
*/
public class AddressUtil {
public static RegionInfo analyticsIp(String content, String encodingString) {
// pconline
String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
// http://whois.pconline.com.cn IP
String returnStr = getResult(urlStr, content, encodingString);
RegionInfo info = null;
if (returnStr != null) {
//
//System.out.println("1: " + returnStr);
String data = JSON.parseObject(returnStr).getString("data");
JSONObject jsonObject = JSON.parseObject(data);
info = new RegionInfo(jsonObject.getString("country"),
jsonObject.getString("region"),
jsonObject.getString("city"));
}
return info;
}
/**
* @param urlStr
* @param content :name=xxx&pwd=xxx
* @param encoding 。 GBK,UTF-8
* @return
*/
private static String getResult(String urlStr, String content, String encoding) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();//
connection.setConnectTimeout(2000);// ,
connection.setReadTimeout(2000);// ,
connection.setDoOutput(true);// true|false
connection.setDoInput(true);// true|false
connection.setRequestMethod("POST");// POST|GET
connection.setUseCaches(false);// true|false
connection.connect();//
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());//
out.writeBytes(content);// , name=xxx&pwd=xxx
out.flush();//
out.close();//
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), encoding));//
// , BufferedReader
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();//
}
}
return null;
}
/**
* unicode
*
* @param theString
* @return
*/
private static String decodeUnicode(String theString) {
char aChar;
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = 0; x < len; ) {
aChar = theString.charAt(x++);
if (aChar == '\\') {
aChar = theString.charAt(x++);
if (aChar == 'u') {
int value = 0;
for (int i = 0; i < 4; i++) {
aChar = theString.charAt(x++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed encoding.");
}
}
outBuffer.append((char) value);
} else {
if (aChar == 't') {
aChar = '\t';
} else if (aChar == 'r') {
aChar = '\r';
} else if (aChar == 'n') {
aChar = '
';
} else if (aChar == 'f') {
aChar = '\f';
}
outBuffer.append(aChar);
}
} else {
outBuffer.append(aChar);
}
}
return outBuffer.toString();
}
/**
*
*/
public static class RegionInfo{
private String country;
private String region;
private String city;
public RegionInfo(){}
public RegionInfo(String country, String region, String city) {
this.country = country;
this.region = region;
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "RegionInfo{" +
"country='" + country + '\'' +
", region='" + region + '\'' +
", city='" + city + '\'' +
'}';
}
}
}
ここで使用されるfastjson jarは、maven依存性が次のようになります.
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.3version>
dependency>
アドレス解析ツールクラスをテストします.
public class TestAddressUtil {
//
public static void main(String[] args) {
AddressUtil addressUtils = new AddressUtil();
// ip
// 219.136.134.157
// 61.244.148.166
// 114.61.94.253
// 222.218.157.234
// 59.125.39.5
String ip = "219.136.134.157";
AddressUtil.RegionInfo info = addressUtils.analyticsIp("ip=" + ip, "utf-8");
System.out.println(info);
}
}