当前位置:网站首页>IP address home location query

IP address home location query

2022-07-07 14:04:00 It is small new

The goal is

By developing IP Address attribution query platform , We need to be right about JavaSE The comprehensive technology has been improved , Enhance actual combat capability . After studying this project, we should have the following abilities :

1 Object oriented programming

2 Tool class encapsulation and usage writing

3 file IO flow

4 string manipulation

5 Binary search

6 IP Different forms of address use

Ideas

1 Read the content in the program

2 analysis IP character string , Structured processing

3 Wrapper utility class

4 Interface API

Enter the reference : IP

The ginseng : Place of ownership

Code development

Read the file

public static List<String>getLineList(String filePath,String encoding)throws IOException{
		// Node stream docking file 
		FileInputStream fis=new FileInputStream(filePath);
		// Convert to character   And specify character encoding  
		Reader reader=new InputStreamReader(fis,encoding);
		// Buffering streams improves efficiency 
		BufferedReader br=new BufferedReader(reader);
		// Read 
		String line=null;
		// Save the read data 
		List<String>lineList=new ArrayList<String>();
		while((line=br.readLine())!=null){
			// Add to collection 
			lineList.add(line);
		}
		// close 
		br.close();
		return lineList;

 

  structured ip Address entity class 
 * 
 *
 */
public class IPAndLocationPojo implements Comparable<IPAndLocationPojo>{
	// Derived fields   Used to hold ip Corresponding long value 
	private long startIPLong;
	private long endIPLong;
	// start IP
	private String startIP;
	// end IP
	private String endIP;
	// Place of ownership 
	private String location;
	 public int compareTo(IPAndLocationPojo o){
		 long status=this.startIPLong-o.startIPLong;
		 // Cannot cast   If the two values differ 2127483647 Words   Convert to int after   Get a negative number 
		 //return(int)(this.start.IPLong-o.startIPLong);
		 return status>0?1:0;
	 }
	public IPAndLocationPojo(long startIPLong, long endIPLong, String startIP,
			String endIP, String location) {
		super();
		// Assign a value to a long integer 
		this.startIPLong = IPUtil.ipToLong(startIP);;
		this.endIPLong = IPUtil.ipToLong(endIP);;
		this.startIP = startIP;
		this.endIP = endIP;
		this.location = location;
	}
	public long getStartIPLong() {
		return startIPLong;
	}
	public void setStartIPLong(long startIPLong) {
		this.startIPLong = startIPLong;
	}
	public long getEndIPLong() {
		return endIPLong;
	}
	public void setEndIPLong(long endIPLong) {
		this.endIPLong = endIPLong;
	}
	public String getStartIP() {
		return startIP;
	}
	public void setStartIP(String startIP) {
		this.startIP = startIP;
	}
	public String getEndIP() {
		return endIP;
	}
	public void setEndIP(String endIP) {
		this.endIP = endIP;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public IPAndLocationPojo() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "IPAndLocationPojo [startIPLong=" + startIPLong + ", endIPLong="
				+ endIPLong + ", startIP=" + startIP + ", endIP=" + endIP
				+ ", location=" + location + "]";
	}

 

  Program core business class 
 * 
 *
 */
public class DataProcessManager {
	private static IPAndLocationPojo[]ipAndLocationPojoArray=null;
	static{
		// File path 
		String ipLibrayPath="ip_location_relation.txt";
		String encoding="UTF-8";
		// Save data object 
		List<IPAndLocationPojo>ipAndLocationPojos=null;
		try {
			//  get data 
			ipAndLocationPojos = DataProcessManager.getPojoList(ipLibrayPath,
					encoding);
			//  Turn the array and sort 
			ipAndLocationPojoArray = DataProcessManager
					.convertListToArraySort(ipAndLocationPojos);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 *  External interface    Participation is ip  The exit is the place of belonging 
	 * @param ipAndLocationPojos
	 * @return
	 */
	public static String getLocation(String ip){
		// Binary search 
		int index=DataProcessManager.binaraySeach(ipAndLocationPojoArray, ip);
		//  Determine if it is found 
				if (index == -1) {
					return null;
				} else {
					return ipAndLocationPojoArray[index].getLocation();
				}
			}
	/**
	 *  Binary search , Participation is IP And an array , The output parameter is the corresponding index , No return found -1;
	 * @param ipAndLocationPojos
	 * @return
	 */
	public static int binaraySeach(IPAndLocationPojo[] ipAndLocationPojoArray,
			String targetIP) {
		//  hold IP Convert to long
		long targetIPLong = IPUtil.ipToLong(targetIP);
		int startIndex = 0;
		int endIndex = ipAndLocationPojoArray.length - 1;
		int m = (startIndex + endIndex) / 2;

		/**
		 *  If   Less than   start IP  Find the front 
		 * 
		 *  If   Greater than   start IP  Find the back 
		 * 
		 *  If   Greater than or equal to start IP And   Less than or equal to   end IP  It says it found it 
		 */
		while (startIndex <= endIndex) {
			if (targetIPLong >= ipAndLocationPojoArray[m].getStartIPLong()
					&& targetIPLong <= ipAndLocationPojoArray[m].getEndIPLong()) {
				return m;
			}
			if (targetIPLong < ipAndLocationPojoArray[m].getStartIPLong()) {
				endIndex = m - 1;
			} else {
				startIndex = m + 1;
			}
			m = (startIndex + endIndex) / 2;
		}
		return -1;
	}
	// Convert the set into an array and sort 
	public static IPAndLocationPojo[]convertListArraySort(List<IPAndLocationPojo>ipAndLocationPojos){
		// Create array 
		IPAndLocationPojo[]ipAndLocationPojoArray=new IPAndLocationPojo[ipAndLocationPojos.size()];
		// Convert to array 
		ipAndLocationPojos.toArray(ipAndLocationPojoArray);
		// Sort 
		Arrays.sort(ipAndLocationPojoArray);
		return ipAndLocationPojoArray;
	}
	// Structured data collection 
	public static List<IPAndLocationPojo>getPojoList(String filePath,String encoding)throws IOException{
		// Save data object 
		List<IPAndLocationPojo>ipAndLocationPojos=new ArrayList<IPAndLocationPojo>();
		List<String>lineList=FileOperatorUtil.getLineList(filePath, encoding);
		for(String string:lineList){
			// Judge whether it is an empty line 
			if(string==null||string.trim().equals("")){
				continue;
			}
			// Split array 
			String[] columnArray=string.split("  ");
			// Get start ip
			String startIP=columnArray[0];
			// End of acquisition ip
			String endIP=columnArray[1];
			// Get the place of belonging 
			String location=columnArray[2];
			// Encapsulate into objects 
			IPAndLocationPojo ipAndLocationPojo=new IPAndLocationPojo(startIP,endIP,location);
			// Add to collection 
			ipAndLocationPojos.add(ipAndLocationPojo);
			
		}
		return ipAndLocationPojos;
// entrance 
public class SystemController {
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		//  Receive user input 
		Scanner scanner = new Scanner(System.in);
		while (true) {
			System.out.println(" Please enter IP Address  : ");
			String ip = scanner.nextLine();
			//  Inquire about 
			long startTime = System.currentTimeMillis();
			String location = DataProcessManager.getLocation(ip);
			long endTime = System.currentTimeMillis();
			System.out.println(" Time consuming  : " + (endTime - startTime) + "  "
					+ location);

原网站

版权声明
本文为[It is small new]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130616018445.html