Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Using ip-attribution.dat File to realize IP belonging place query

Shulou Source: shulou.com Published: 2022-06-02 09:46:22 10月03日 Update

This article mainly introduces "using ip-attribution.dat files to achieve IP attribution query". In daily operation, I believe many people have doubts about using ip-attribution.dat files to achieve IP attribution query. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "using ip-attribution.dat files to achieve IP attribution query". Next, please follow the editor to study!

Use ip-attribution.dat file to implement IP attribution query. Download address

Download the extraction code of the network disk: 2chr

Package com.sky.common.utils;import cn.hutool.core.io.resource.ClassPathResource;import org.apache.commons.lang3.StringUtils;import java.io.*;import java.net.Inet4Address;import java.net.InetAddress;import java.net.UnknownHostException;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.util.Arrays;import java.util.HashMap;import java.util.Map / * ip Home Resolution * * @ author Moses wsj1198878990@126.com * @ since 1.0.0 2019-10-22 * / public final class IPDataHandler {private static DataInputStream inputStream = null; private static long fileLength =-1; private static int dataLength =-1; private static Map cacheMap = null; private static byte [] allData = null; static {/ / File file = new File (IP_DATA_PATH) Try {/ / put the file under src/main/resources File file = new ClassPathResource ("ip-attribution.dat") .getFile (); inputStream = new DataInputStream (new FileInputStream (file)); fileLength = file.length (); cacheMap = new HashMap () If (fileLength > Integer.MAX_VALUE) {throw new Exception ("the filelength over 2GB");} dataLength = (int) fileLength; allData = new byte [dataLength]; inputStream.read (allData, 0, dataLength); dataLength = (int) getbytesTolong (allData, 0,4, ByteOrder.BIG_ENDIAN) } catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} catch (Exception e) {e.printStackTrace () }} private static long getbytesTolong (byte [] bytes, int offerSet, int size, ByteOrder byteOrder) {if ((offerSet + size) > bytes.length | | size 255) {return "illegal ip";} if (cacheMap.containsKey (ip)) {return cacheMap.get (ip);} long numIp = 1 Try {numIp = ip2long (address);} catch (UnknownHostException E1) {e1.printStackTrace ();} int tempOffSet = ipHeadValue * 4 + 4; long start = getbytesTolong (allData, tempOffSet, 4, ByteOrder.LITTLE_ENDIAN); int max_len = dataLength-1028; long resultOffSet = 0; int resultSize = 0 For (start = start * 8 + 1024; start

< max_len; start += 8) { if (getbytesTolong(allData, (int) start + 4, 4, ByteOrder.BIG_ENDIAN) >

= numIp) {resultOffSet = getIntByBytes (allData, (int) (start + 4 + 4)); resultSize = (char) allData [(int) start + 7 + 4]; break;}} if (resultOffSet

Tags: File attribution query learning more help practical next address article method theory knowledge article website material follow question UTF-8 easy to use Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux Redmi Huawei macOS