Baru-baru ini, fungsi tertentu dari proyek perlu mendapatkan lokasi geografis terperinci dari antarmuka pihak ketiga berdasarkan alamat IP. Saya telah menemukan banyak contoh dari internet, termasuk Sina, Taobao, dan Tencent. Saya mencoba Taobao. Jika itu adalah urutan kecil yang besar, tidak apa -apa. Jika mencapai tingkat 100.000, kecepatannya akan lambat, yang akan menyebabkan sistem macet. Contoh berikut adalah dari Sina. Contohnya tidak cocok untuk setiap proyek dan perlu dimodifikasi.
/** ipsearchUrl = http: //int.dpool.sina.com.cn/iplookup/iplookup.php? Format = js & ip = (Ini adalah alamat antarmuka SINA) dalam proyek, alamat antarmuka SINA tidak ditulis secara langsung, tetapi file atribut dibaca. */ string statis public getipinfobysina (string ip) {// Baca file atribut (file-value dan format file properti) string final prop_ipsearchurl = "ipsearchUrl"; string akhir ret_success = "1"; string final ret = "ret"; final string province = "provinsi"; Final String City = "City"; Final String District = "District"; string terakhir isp = "ISP"; String ipaddress = ""; if (stringutils.isblank (ip)) {return null; } String url = systemparampropertyutils.getsystemparamkeyValue (prop_ipsearchurl); // Ini untuk mendapatkan URL dari file properti, yaitu, Alamat antarmuka SINA if (stringutils.isnotblank (url)) {string path = url+ip; // "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip="+ip; HttpClient httpClient = httpClient baru (); Peta <string, string> parammap = hashmap baru <string, string> (); String remoteipInfo = ""; coba {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, provinsi); 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, tombol string) {string _value = ""; if (stringutils.isnotblank (remoteipInfo)) {_value = stringutils.substringbetween (remoteipInfo, "/" "+key+"/":", ","); if (stringutils.isnotblank (_value)) {_value = unicodeutils.decodeunicode (_value); }} return _value; } Saya membaca alamat antarmuka SINA dengan sangat cepat. Saya mengujinya sekitar 90.000, sepuluh menit. Untuk Taobao, lebih dari satu jam, lebih dari 50.000 buah. Ada trik lain, yaitu menyimpan IP baca ke dalam peta, dan kemudian keluarkan langsung dari peta jika Anda membacanya lain kali, yang jauh lebih cepat. Ini akan menyebabkan banyak masalah. Ketika file log mencapai jutaan atau puluhan juta, bagaimana cara menyelesaikannya? Untuk contoh seperti Taobao, setiap pesanan berbeda dalam jumlah pesanan dalam satu detik. Saya tidak tahu cara menyelesaikannya. Beberapa Tuhan yang hebat tahu untuk membalas saya.
Berikut ini adalah Taobao.
Impor java.io.bufferedReader; impor java.io.dataoutputstream; impor java.io.ioexception; impor java.io.inputStreamReader; impor java.io.unsupportedencePortedException; Impor Java.net.htpurlconnection; Alamat IP * @Author lwl * @DateJan 26, 2016 * /Alamat kelas publik { /** * @param konten * Format parameter yang diminta adalah: name = xxx & pwd = xxx * @param encoding * Pengkodean permintaan sisi server. Misalnya, GBK, UTF-8, dll. * @Return * @Throws UnsupportedencodingException */Public String getAddresses (konten string, string encodingstring) melempar UnsupportedEncodingException {// hubungi PCONLINE INTERFACE URLST = "http://ip.taobao. // Dapatkan informasi provinsi, kota, dan distrik di mana IP terletak dari http://whois.pconline.com.cn string returnstr = this.getResult (urlstr, konten, encodingstring); if (returnStr! = null) {// Proses sistem informasi provinsi dan kota yang dikembalikan.out.println (returnStr); String [] temp = returnstr.split (","); if (temp.length <3) {return "0"; // tidak valid ip, uji lan} string region = (temp [5] .split (":")) [1] .replaceall ("/" "," ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ""; ""; "" ";" ""; "" ";" ""; "" ";" ""; "" ";" ""; "" ";" ""; "" ";" ""; "" ";" ""; "" ";" ""; "" ";" ""; "" ";" ""; "" ";" "; = ""; untuk (int i = 0; i <temp.length; i ++) {switch (i) {case 1: country = (temp [i] .split (":")) [2] .replaceall ("/" "," "); country = decodeunicode (negara); // istirahat negara; Kasus 3: Area = (Temp [i] .split (":")) [1] .replaceall ("/" "," "); Area = decodeunicode (area); // Break Wilayah; Kasus 5: Region = (Temp [i] .split (": ")) [1]. Replaceall ("/"," wilayah = decodeunicode (wilayah); // Provinsi istirahat; Kasus 7: City = (temp [i] .split (":")) [1] .replaceall ("/" "," "); city = decodeunicode (kota); // break kota; kasus 9: county = (temp [i] .split (": ")) [1]. Replaceall ("/"," " county = decodeUnicode (county); // Break wilayah; Kasus 11: ISP = (temp [i] .split (":")) [1] .replaceall ("/" "," "); ISP = decodeUnicode (ISP); // ISP Perusahaan Istirahat;}} System.out.println (negara+wilayah+wilayah+kota+county+iSp) baru) baru) StringBuffer (Negara). Laporan (Area). Laporan (Region). Laporan (City) .Perpen (county). Laporan (ISP) .TOSTRING (); GBK, UTF-8, dll. * @Return */ Private String getResult (string urlstr, konten string, encoding string) {url url = null; Connection.setConnectTimeOut (2000); // Tetapkan waktu batas waktu koneksi dalam koneksi milidonds.setreadtimeout (33000); /// Tetapkan batas waktu untuk membaca data, dalam koneksi Milliseconds.setDoOutput (true); // apakah akan membuka aliran output true | false connection.setDoPut (true);/apakah akan akan membuka aliran FALSE FALSE | ESTOME FALSE. Kirim Metode Posting | Dapatkan Connection.setUsecaches (false); // apakah akan cache true | false connection.connect (); // buka port koneksi dataOutputStream out = new DataOutputStream (connection.getOutputStream ()); // buka aliran output dan tulis data ke server peer out.writeBytes (content); // data output dan write tanda itu, tanda itu, name/ write form out. out.flush (); // Refresh out.close (); // Tutup aliran output BufferedReader Reader = BufferedReader baru (inputStreamReader baru (connection.getInputStream (), encoding)); // Setelah menulis data ke server peer, pengembalian data //, gunakan stream buffered feaming untuk dibaca StringBuffering Buffer Buffer = Return Data/, Gunakan streaming BufferedReader untuk membacakan StringBuffer String line = ""; while ((line = reader.readline ())! = null) {buffer.append (line); } reader.close (); return buffer.toString (); } catch (ioException e) {e.printstacktrace (); } akhirnya {if (koneksi! = null) {connection.disconnect (); // tutup koneksi}} return null; } / ** * Unicode Convert ke China * * @Author FanHui 2007-3-15 * @param thestring * @return * / public static string decodeUnicode (string thestring) {char achar; int len = thestring.length (); StringBuffer outbuffer = StringBuffer baru (len); untuk (int x = 0; x <len;) {achar = thestring.charat (x ++); if (achar == '//') {achar = thestring.charat (x ++); if (achar == 'u') {int value = 0; untuk (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'; merusak; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': value = (value << 4) + 10 + achar - 'a'; merusak; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': value = (value << 4) + 10 + achar - 'a'; merusak; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': value = (value << 4) + 10 + achar - 'a'; merusak; Default: Lempar IllegalArgumentException baru ("Pengkodean Malformed."); }} outbuffer.append ((char) value); } else {if (achar == 't') {achar = '/t'; } lain jika (achar == 'r') {achar = '/r'; } lain jika (achar == 'n') {achar = '/n'; } lain jika (achar == 'f') {achar = '/f'; } outbuffer.append (achar); }} else {outbuffer.append (achar); }} return outbuffer.toString (); } // uji public static void main (string [] args) {addressutils addressutils = new addressUtils (); // Tes IP 219.136.134.157 Cina = Cina Selatan = Provinsi Guangdong = Kota Guangzhou = Distrik Yuexiu = Telecom String IP = "125.70.11.136"; Alamat String = ""; coba {address = addressutils.getAddresses ("ip ="+ip, "UTF-8"); } catch (UnsupportedEncodingException e) {// TODO Auto-Enchenerated Catch Block E.PrintStackTrace (); } System.out.println (alamat); // Hasil outputnya adalah: Provinsi Guangdong, Kota Guangzhou, Distrik Yuexiu}}