java根據(jù)ip地址獲取詳細(xì)地域信息的方法
互聯(lián)網(wǎng)有很多接口可以實(shí)現(xiàn)通過ip查詢到具體的位置,如下:
通過淘寶IP地址庫(kù)獲取IP位置
請(qǐng)求接口(GET):http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]
響應(yīng)信息:(json格式的)國(guó)家 、?。ㄗ灾螀^(qū)或直轄市)、市(縣)、運(yùn)營(yíng)商
返回?cái)?shù)據(jù)格式:
{“code”:0,”data”:{“ip”:”210.75.225.254”,”country”:”\u4e2d\u56fd”,”area”:”\u534e\u5317”,
“region”:”\u5317\u4eac\u5e02”,”city”:”\u5317\u4eac\u5e02”,”county”:”“,”isp”:”\u7535\u4fe1”,
“country_id”:”86”,”area_id”:”100000”,”region_id”:”110000”,”city_id”:”110000”,
“county_id”:”-1”,”isp_id”:”100017”}}
其中code的值的含義為,0:成功,1:失敗。
新浪的接口 :http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.192.3.42
返回值
var remote_ip_info = {“ret”:1,”start”:”218.192.0.0”,”end”:”218.192.7.255”,”country”:”\u4e2d\u56fd”,”province”:”\u5e7f\u4e1c”,”city”:”\u5e7f\u5dde”,”district”:”“,”isp”:”\u6559\u80b2\u7f51”,”type”:”\u5b66\u6821”,”desc”:”\u5e7f\u5dde\u5927\u5b66\u7eba\u7ec7\u670d\u88c5\u5b66\u9662”};
通過jqry 獲取相應(yīng)的數(shù)據(jù)
$.getScript(‘?dāng)?shù)據(jù)接口',function(){
//新浪:remote_ip_info.country
})
騰訊IP分享計(jì)劃的地址獲取IP所在地: http://ip.qq.com/cgi-bin/searchip?searchip1=ip
用Java調(diào)用淘寶ip查詢接口查詢地域的一個(gè)java實(shí)例:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 根據(jù)IP地址獲取詳細(xì)的地域信息
* @project:personGocheck
* @class:AddressUtils.java
* @author:heguanhua E-mail:37809893@qq.com
* @date:Nov 14, 2012 6:38:25 PM
*/
public class AddressUtils {
/**
*
* @param content
* 請(qǐng)求的參數(shù) 格式為:name=xxx&pwd=xxx
* @param encoding
* 服務(wù)器端請(qǐng)求編碼。如GBK,UTF-8等
* @return
* @throws UnsupportedEncodingException
*/
public String getAddresses(String content, String encodingString)
throws UnsupportedEncodingException {
// 這里調(diào)用pconline的接口
String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
// 從http://whois.pconline.com.cn取得IP所在的省市區(qū)信息
String returnStr = this.getResult(urlStr, content, encodingString);
if (returnStr != null) {
// 處理返回的省市區(qū)信息
System.out.println(returnStr);
String[] temp = returnStr.split(",");
if(temp.length<3){
return "0";//無效IP,局域網(wǎng)測(cè)試
}
String region = (temp[5].split(":"))[1].replaceAll("\"", "");
region = decodeUnicode(region);// 省份
String country = "";
String area = "";
// String region = "";
String city = "";
String county = "";
String isp = "";
for (int i = 0; i < temp.length; i++) {
switch (i) {
case 1:
country = (temp[i].split(":"))[2].replaceAll("\"", "");
country = decodeUnicode(country);// 國(guó)家
break;
case 3:
area = (temp[i].split(":"))[1].replaceAll("\"", "");
area = decodeUnicode(area);// 地區(qū)
break;
case 5:
region = (temp[i].split(":"))[1].replaceAll("\"", "");
region = decodeUnicode(region);// 省份
break;
case 7:
city = (temp[i].split(":"))[1].replaceAll("\"", "");
city = decodeUnicode(city);// 市區(qū)
break;
case 9:
county = (temp[i].split(":"))[1].replaceAll("\"", "");
county = decodeUnicode(county);// 地區(qū)
break;
case 11:
isp = (temp[i].split(":"))[1].replaceAll("\"", "");
isp = decodeUnicode(isp); // ISP公司
break;
}
}
System.out.println(country+"="+area+"="+region+"="+city+"="+county+"="+isp);
return region;
}
return null;
}
/**
* @param urlStr
* 請(qǐng)求的地址
* @param content
* 請(qǐng)求的參數(shù) 格式為:name=xxx&pwd=xxx
* @param encoding
* 服務(wù)器端請(qǐng)求編碼。如GBK,UTF-8等
* @return
*/
private String getResult(String urlStr, String content, String encoding) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();// 新建連接實(shí)例
connection.setConnectTimeout(2000);// 設(shè)置連接超時(shí)時(shí)間,單位毫秒
connection.setReadTimeout(2000);// 設(shè)置讀取數(shù)據(jù)超時(shí)時(shí)間,單位毫秒
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());// 打開輸出流往對(duì)端服務(wù)器寫數(shù)據(jù)
out.writeBytes(content);// 寫數(shù)據(jù),也就是提交你的表單 name=xxx&pwd=xxx
out.flush();// 刷新
out.close();// 關(guān)閉輸出流
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), encoding));// 往對(duì)端寫完數(shù)據(jù)對(duì)端服務(wù)器返回?cái)?shù)據(jù)
// ,以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();// 關(guān)閉連接
}
}
return null;
}
/**
* unicode 轉(zhuǎn)換成 中文
*
* @author fanhui 2007-3-15
* @param theString
* @return
*/
public 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 = '\n';
} else if (aChar == 'f') {
aChar = '\f';
}
outBuffer.append(aChar);
}
} else {
outBuffer.append(aChar);
}
}
return outBuffer.toString();
}
// 測(cè)試
public static void main(String[] args) {
AddressUtils addressUtils = new AddressUtils();
// 測(cè)試ip 219.136.134.157 中國(guó)=華南=廣東省=廣州市=越秀區(qū)=電信
String ip = "125.70.11.136";
String address = "";
try {
address = addressUtils.getAddresses("ip="+ip, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(address);
// 輸出結(jié)果為:廣東省,廣州市,越秀區(qū)
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Feign Client 超時(shí)時(shí)間配置不生效的解決
這篇文章主要介紹了Feign Client 超時(shí)時(shí)間配置不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
JDK17在Windows安裝及環(huán)境變量配置超詳細(xì)的教程
這篇文章主要介紹了JDK17在Windows安裝及環(huán)境變量配置超詳細(xì)的教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11
SpringMVC結(jié)合模板模式實(shí)現(xiàn)MyBatisPlus傳遞嵌套JSON數(shù)據(jù)
我們經(jīng)常會(huì)遇到需要傳遞對(duì)象的場(chǎng)景,有時(shí)候,我們需要將一個(gè)對(duì)象的數(shù)據(jù)傳遞給另一個(gè)對(duì)象進(jìn)行處理,但是又不希望直接暴露對(duì)象的內(nèi)部結(jié)構(gòu)和實(shí)現(xiàn)細(xì)節(jié),所以本文給大家介紹了SpringMVC結(jié)合模板模式實(shí)現(xiàn)MyBatisPlus傳遞嵌套JSON數(shù)據(jù),需要的朋友可以參考下2024-03-03
Java對(duì)象轉(zhuǎn)json JsonFormat注解
這篇文章主要介紹了Java對(duì)象轉(zhuǎn)json JsonFormat注解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
基于Springboot+Junit+Mockito做單元測(cè)試的示例
本篇文章主要介紹了基于Springboot+Junit+Mockito做單元測(cè)試的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02

