AddressUtils.java 8.4 KB

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