AddressUtils.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package com.yihu.jw.util;
  2. import org.apache.commons.lang.StringUtils;
  3. import java.io.*;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. /**
  7. * Created by Administrator on 2017/6/6 0006.
  8. */
  9. public class AddressUtils {
  10. public String getAddresses(String ip)
  11. throws UnsupportedEncodingException {
  12. return getAddresses(ip,"utf-8");
  13. }
  14. /**
  15. * 返回"0" 为无效ip,局域网测试
  16. * "中国-西南-四川省-成都市- -电信" (没有值,中间用空格隔开 country-area-region-city-county-isp)
  17. * @param content
  18. * @param encodingString
  19. * @return
  20. * @throws UnsupportedEncodingException
  21. */
  22. public String getAddresses(String content, String encodingString)
  23. throws UnsupportedEncodingException {
  24. // 这里调用pconline的接口
  25. String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
  26. // 从http://whois.pconline.com.cn取得IP所在的省市区信息
  27. content = "ip="+content;
  28. String returnStr = this.getResult(urlStr, content, encodingString);
  29. if (returnStr != null) {
  30. // 处理返回的省市区信息
  31. System.out.println(returnStr);
  32. String[] temp = returnStr.split(",");
  33. if (temp.length < 3) {
  34. return "0";//无效IP,局域网测试
  35. }
  36. String region = (temp[5].split(":"))[1].replaceAll("\"", "");
  37. region = decodeUnicode(region);// 省份
  38. String country = "";
  39. String area = "";
  40. // String region = "";
  41. String city = "";
  42. String county = "";
  43. String isp = "";
  44. for (int i = 0; i < temp.length; i++) {
  45. switch (i) {
  46. case 1:
  47. country = (temp[i].split(":"))[2].replaceAll("\"", "");
  48. country = decodeUnicode(country);// 国家
  49. break;
  50. case 3:
  51. area = (temp[i].split(":"))[1].replaceAll("\"", "");
  52. area = decodeUnicode(area);// 地区
  53. break;
  54. case 5:
  55. region = (temp[i].split(":"))[1].replaceAll("\"", "");
  56. region = decodeUnicode(region);// 省份
  57. break;
  58. case 7:
  59. city = (temp[i].split(":"))[1].replaceAll("\"", "");
  60. city = decodeUnicode(city);// 市区
  61. break;
  62. case 9:
  63. county = (temp[i].split(":"))[1].replaceAll("\"", "");
  64. county = decodeUnicode(county);// 地区
  65. break;
  66. case 11:
  67. isp = (temp[i].split(":"))[1].replaceAll("\"", "");
  68. isp = decodeUnicode(isp); // ISP公司
  69. break;
  70. }
  71. }
  72. String district = country + "-" + area + "-" + region + "-" + city + "-" + county + "-" + isp;
  73. return district;
  74. }
  75. return null;
  76. }
  77. /**
  78. * @param urlStr 请求的地址
  79. * @param content 请求的参数 格式为:name=xxx&pwd=xxx
  80. * @param encoding 服务器端请求编码。如GBK,UTF-8等
  81. * @return
  82. */
  83. private String getResult(String urlStr, String content, String encoding) {
  84. URL url = null;
  85. HttpURLConnection connection = null;
  86. try {
  87. url = new URL(urlStr);
  88. connection = (HttpURLConnection) url.openConnection();// 新建连接实例
  89. connection.setConnectTimeout(2000);// 设置连接超时时间,单位毫秒
  90. connection.setReadTimeout(2000);// 设置读取数据超时时间,单位毫秒
  91. connection.setDoOutput(true);// 是否打开输出流 true|false
  92. connection.setDoInput(true);// 是否打开输入流true|false
  93. connection.setRequestMethod("POST");// 提交方法POST|GET
  94. connection.setUseCaches(false);// 是否缓存true|false
  95. connection.connect();// 打开连接端口
  96. DataOutputStream out = new DataOutputStream(connection
  97. .getOutputStream());// 打开输出流往对端服务器写数据
  98. out.writeBytes(content);// 写数据,也就是提交你的表单 name=xxx&pwd=xxx
  99. out.flush();// 刷新
  100. out.close();// 关闭输出流
  101. BufferedReader reader = new BufferedReader(new InputStreamReader(
  102. connection.getInputStream(), encoding));// 往对端写完数据对端服务器返回数据
  103. // ,以BufferedReader流来读取
  104. StringBuffer buffer = new StringBuffer();
  105. String line = "";
  106. while ((line = reader.readLine()) != null) {
  107. buffer.append(line);
  108. }
  109. reader.close();
  110. return buffer.toString();
  111. } catch (IOException e) {
  112. e.printStackTrace();
  113. } finally {
  114. if (connection != null) {
  115. connection.disconnect();// 关闭连接
  116. }
  117. }
  118. return null;
  119. }
  120. /**
  121. * unicode 转换成 中文
  122. *
  123. * @param theString
  124. * @return
  125. * @author fanhui 2007-3-15
  126. */
  127. public static String decodeUnicode(String theString) {
  128. char aChar;
  129. int len = theString.length();
  130. StringBuffer outBuffer = new StringBuffer(len);
  131. for (int x = 0; x < len; ) {
  132. aChar = theString.charAt(x++);
  133. if (aChar == '\\') {
  134. aChar = theString.charAt(x++);
  135. if (aChar == 'u') {
  136. int value = 0;
  137. for (int i = 0; i < 4; i++) {
  138. aChar = theString.charAt(x++);
  139. switch (aChar) {
  140. case '0':
  141. case '1':
  142. case '2':
  143. case '3':
  144. case '4':
  145. case '5':
  146. case '6':
  147. case '7':
  148. case '8':
  149. case '9':
  150. value = (value << 4) + aChar - '0';
  151. break;
  152. case 'a':
  153. case 'b':
  154. case 'c':
  155. case 'd':
  156. case 'e':
  157. case 'f':
  158. value = (value << 4) + 10 + aChar - 'a';
  159. break;
  160. case 'A':
  161. case 'B':
  162. case 'C':
  163. case 'D':
  164. case 'E':
  165. case 'F':
  166. value = (value << 4) + 10 + aChar - 'A';
  167. break;
  168. default:
  169. throw new IllegalArgumentException(
  170. "Malformed encoding.");
  171. }
  172. }
  173. outBuffer.append((char) value);
  174. } else {
  175. if (aChar == 't') {
  176. aChar = '\t';
  177. } else if (aChar == 'r') {
  178. aChar = '\r';
  179. } else if (aChar == 'n') {
  180. aChar = '\n';
  181. } else if (aChar == 'f') {
  182. aChar = '\f';
  183. }
  184. outBuffer.append(aChar);
  185. }
  186. } else {
  187. outBuffer.append(aChar);
  188. }
  189. }
  190. String string = outBuffer.toString();
  191. if(StringUtils.isBlank(string)){
  192. string=" ";
  193. }
  194. return string;
  195. }
  196. // 测试
  197. public static void main(String[] args) {
  198. AddressUtils addressUtils = new AddressUtils();
  199. // 测试ip 219.136.134.157 中国-华南-广东省-广州市-越秀区-电信
  200. String ip = "219.136.134.157";
  201. String address = "";
  202. try {
  203. address = addressUtils.getAddresses(ip);
  204. } catch (UnsupportedEncodingException e) {
  205. // TODO Auto-generated catch block
  206. e.printStackTrace();
  207. }
  208. System.out.println(address);
  209. // 输出结果为:广东省,广州市,越秀区
  210. }
  211. }