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