Recently, a certain function of the project needs to obtain detailed geographical location from a third-party interface based on the IP address. I have found many examples from the Internet, including Sina, Taobao, and Tencent. I tried Taobao, if it is a small order of magnitude, it is OK, but if it reaches a level of 100,000, the speed will be slow, which will cause the system to crash. The following example is from Sina. The example is not suitable for each project and needs to be modified.
/** ipSearchUrl=http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip= (this is Sina's interface address) In the project, Sina's interface address is not written directly, but the attribute file is read. */ public static String getIpInfoBySina(String ip){ // Read the attribute file (property file key-value and format) final String PROP_IPSEARCHURL="ipSearchUrl"; final String RET_SUCCESS="1"; final String RET="ret"; final String PROVINCE="province"; final String CITY="city"; final String DISTRICT="district"; final String ISP="isp"; String ipAddress=""; if(StringUtils.isBlank(ip)){ return null; } String url = SystemParamPropertyUtils.getSystemParamKeyValue(PROP_IPSEARCHURL);//This is to get the url from the properties file, that is, Sina interface address if(StringUtils.isNotBlank(url)){ String path=url+ip;//"http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip="+ip; HttpClient httpClient = new HttpClient(); Map<String, String> paramMap = new HashMap<String, String>(); String remoteIpInfo=""; try { remoteIpInfo = HttpClientUtil.request(httpClient, path, paramMap,"sina"); } catch (Exception e) { e.printStackTrace(); } if(StringUtils.isNotBlank(remoteIpInfo)){ String _ret=searchValue(remoteIpInfo, RET); if(RET_SUCCESS.equals(_ret)){ String provinceName=searchValue(remoteIpInfo, PROVINCE); String cityName=searchValue(remoteIpInfo, CITY); String district=searchValue(remoteIpInfo, DISTRICT); String isp=searchValue(remoteIpInfo, ISP); ipAddress=provinceName+cityName+district+isp; } } } return ipAddress; } private static String searchValue(String remoteIpInfo,String key){ String _value=""; if(StringUtils.isNotBlank(remoteIpInfo)){ _value=StringUtils.substringBetween(remoteIpInfo,"/""+key+"/":", ","); if(StringUtils.isNotBlank(_value)){ _value=UnicodeUtils.decodeUnicode(_value); } } return _value; } I read the Sina interface address very quickly. I tested it for about 90,000, ten minutes. For Taobao, more than an hour, more than 50,000 pieces. There is another trick, which is to save the read ip into the map, and then take it out directly from the map if you read it next time, which is much faster. This will lead to many problems. When the log files reach the level of millions or tens of millions, how to solve them? For examples like Taobao, each order is different in number of orders in one second. I don't know how to solve it. Some great god knows to reply to me.
The following is Taobao.
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;/** * Get detailed geographical information based on the IP address* @author Lwl* @dateJan 26, 2016 */public class AddressUtils { /** * @param content * The requested parameter format is: name=xxx&pwd=xxx * @param encoding * Server-side request encoding. For example, GBK, UTF-8, etc. * @return * @throws UnsupportedEncodingException */ public String getAddresses(String content, String encodingString) throws UnsupportedEncodingException { // Call the pconline interface String urlStr = "http://ip.taobao.com/service/getIpInfo.php"; // Get the information of the province, city and district where the IP is located from http://whois.pconline.com.cn String returnStr = this.getResult(urlStr, content, encodingString); if (returnStr != null) { // Process the returned provincial and municipal information System.out.println(returnStr); String[] temp = returnStr.split(","); if(temp.length<3){ return "0";//Invalid IP, LAN test} String region = (temp[5].split(":"))[1].replaceAll("/"", ""); region = decodeUnicode(region);// Province 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);// Country break; case 3: area = (temp[i].split(":"))[1].replaceAll("/"", ""); area = decodeUnicode(area);// Region break; case 5: region = (temp[i].split(":"))[1].replaceAll("/"", ""); region = decodeUnicode(region);// Province break; case 7: city = (temp[i].split(":"))[1].replaceAll("/"", ""); city = decodeUnicode(city);// City break; case 9: county = (temp[i].split(":"))[1].replaceAll("/"", ""); county = decodeUnicode(county);// Region break; case 11: isp = (temp[i].split(":"))[1].replaceAll("/"", ""); isp = decodeUnicode(isp); // ISP company break; } } System.out.println(country+area+region+city+county+isp); return new StringBuffer(country).append(area).append(region).append(city).append(county).append(isp).toString(); } return null; } /** * @param urlStr * Requested address* @param content * The requested parameter format is: name=xxx&pwd=xxx * @param encoding * Server-side request encoding. For example, GBK, UTF-8, etc. * @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();// Create a new connection instance connection.setConnectTimeout(2000);// Set the connection timeout time in milliseconds connection.setReadTimeout(33000);/// Set the timeout for reading data, in milliseconds connection.setDoOutput(true);// Whether to open the output stream true|false connection.setDoInput(true);// Whether to open the input stream true|false connection.setRequestMethod("POST");// Submit method POST|GET connection.setUseCaches(false);// Whether to cache true|false connection.connect();// Open the connection port DataOutputStream out = new DataOutputStream(connection.getOutputStream());// Open the output stream and write data to the peer server out.writeBytes(content);// Write data, that is, submit your form name=xxx&pwd=xxx out.flush();// Refresh out.close();// Close the output stream BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));// After writing data to the peer server, return data//, use the BufferedReader stream to read 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();// Close the connection} } return null; } /** * unicode Convert to Chinese * * @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; 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(); } // Test public static void main(String[] args) { AddressUtils addressUtils = new AddressUtils(); // Test ip 219.136.134.157 China = South China = Guangdong Province = Guangzhou City = Yuexiu District = Telecom 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); // The output result is: Guangdong Province, Guangzhou City, Yuexiu District} }