溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用java如何實現(xiàn)根據(jù)ip地址獲取詳細信息

發(fā)布時間:2020-11-09 17:23:27 來源:億速云 閱讀:453 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)利用java如何實現(xiàn)根據(jù)ip地址獲取詳細信息,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

互聯(lián)網(wǎng)有很多接口可以實現(xiàn)通過ip查詢到具體的位置,如下:

通過淘寶IP地址庫獲取IP位置

請求接口(GET):http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]

響應(yīng)信息:(json格式的)國家 、省(自治區(qū)或直轄市)、市(縣)、運營商

返回數(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(‘數(shù)據(jù)接口',function(){ 
//新浪:remote_ip_info.country 
})

騰訊IP分享計劃的地址獲取IP所在地: http://ip.qq.com/cgi-bin/searchip?searchip1=ip

用Java調(diào)用淘寶ip查詢接口查詢地域的一個java實例:

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地址獲取詳細的地域信息
 * @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
 *   請求的參數(shù) 格式為:name=xxx&pwd=xxx
 * @param encoding
 *   服務(wù)器端請求編碼。如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)測試
 }
 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);// 國家
     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
 *   請求的地址
 * @param content
 *   請求的參數(shù) 格式為:name=xxx&pwd=xxx
 * @param encoding
 *   服務(wù)器端請求編碼。如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();// 新建連接實例
 connection.setConnectTimeout(2000);// 設(shè)置連接超時時間,單位毫秒
 connection.setReadTimeout(2000);// 設(shè)置讀取數(shù)據(jù)超時時間,單位毫秒
 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());// 打開輸出流往對端服務(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));// 往對端寫完數(shù)據(jù)對端服務(wù)器返回數(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();
 }
 // 測試
 public static void main(String[] args) {
 AddressUtils addressUtils = new AddressUtils();
 // 測試ip 219.136.134.157 中國=華南=廣東省=廣州市=越秀區(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ū)
 }
} 

看完上述內(nèi)容,你們對利用java如何實現(xiàn)根據(jù)ip地址獲取詳細信息有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI