当前位置: 首页 > news >正文

网站建设优化西安中国风网页设计欣赏

网站建设优化西安,中国风网页设计欣赏,网站制作赚钱吗,商务网站建设的基本步骤一、项目介绍 1. 背景 IP地址是网络通信中的重要标识#xff0c;通过分析IP地址的归属地信息#xff0c;可以帮助我们了解访问来源、用户行为和网络安全等关键信息。例如应用于网站访问日志分析#xff1a;通过分析访问日志中的IP地址#xff0c;了解网站访问者的地理位置分… 一、项目介绍 1. 背景 IP地址是网络通信中的重要标识通过分析IP地址的归属地信息可以帮助我们了解访问来源、用户行为和网络安全等关键信息。例如应用于网站访问日志分析通过分析访问日志中的IP地址了解网站访问者的地理位置分布和访问行为优化网站内容和用户体验。 2. 需求 IP分析返回归属地信息要求在毫秒内完成。 3. 涉及技术栈 Eclipse的使用JavaSE中面向对象IO流二分法查找集合。 4. 目的 通过IP归属地查询项目巩固javaSE部分所学知识增强实战能力。 需具备以下能力1.面向对象程序设计。 2.工具类的封装与使用。 3. 文件IO流操作。 4. 字符串处理。 5. 二分法查找。 二、主要思路 1. 读取提供的地址库文件解析地址库字符串转换为结构化数据 2. 封装对应的数据结构进行查询。 3. 封装对应的相关工具类。 4. 对外提供接口只需要入参和出参入参IP地址出参归属地。 三、代码开发 创建utils包存放工具类 创建pojo包存放实体类 创建manager包存储管理类业务代码 创建controller包存储程序入口类 1.  文件读取封装为工具类 编写文件读取工具类用于读取本地IP地址库文件中的数据。当前类放于Utils包下。 package com.zh.utils;import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.List;public class FileReadUtil {// 默认编码方式private static String defaultencoding UTF-8;public static ListString FileRead(String filePath, String encoding) throws IOException{// 通过类加载器获取流防止打包后无法获取IP文件InputStream is FileReadUtil.class.getClassLoader().getResourceAsStream(filePath);// 字节流// FileInputStream fis new FileInputStream(filePath);// 转字符流Reader read new InputStreamReader(is,encoding);// 缓冲流BufferedReader br new BufferedReader(read);String temp null;// 集合存储数据ListString list new ArrayListString();// 逐行读取while((temp br.readLine()) ! null) {list.add(temp);}br.close();return list;}// 提供无参的方法以默认编码方式为参数调用有参的方法public static ListString FileRead(String filePath) throws IOException{return FileRead(filePath,defaultencoding);}} 2.  抽象Pojo类用于数据结构化 根据地址库中的IP地址及其归属地的存储格式(1.0.0.0    1.0.0.255    澳大利亚 亚太互联网络信息中心)创建实体类。实体类属性包括IP起始地址IP结束地址归属地IP起始地址的long类型数值IP结束地址的long类型数值。IP地址转换为long类型用于后续对IP地址进行排序。 实体类实现Comparable接口用于自定义比较规则覆写comparaTo方法根据起始IP地址进行比较。 实体类实现Serializable接口可对当前对象进行序列化操作。 package com.zh.pojo;import java.io.Serializable;import com.zh.utils.IPUtil;public class IPAndLocationPojo implements ComparableIPAndLocationPojo, Serializable {private static final long serialVersionUID 1L;/*** 起始IP*/private String startIP;/*** 结束IP*/private String endIP;/*** 归属地*/private String location;/*** 起始地址转long类型*/private long startIPLong;/*** 结束地址转long类型*/private long endIPLong;public IPAndLocationPojo() {super();}public IPAndLocationPojo(String startIP, String endIP, String location) {super();this.startIP startIP;this.endIP endIP;this.location location;this.startIPLong IPUtil.ipToLong(startIP);this.endIPLong IPUtil.ipToLong(endIP);}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 long getStartIPLong() {return startIPLong;}public long getEndIPLong() {return endIPLong;}Overridepublic String toString() {return IPAndLocationPojo [startIP startIP , endIP endIP , location location , startIPLong startIPLong , endIPLong endIPLong ];}Overridepublic int compareTo(IPAndLocationPojo o) {long result this.startIPLong - o.getStartIPLong();if(result 0) {return 1; // 正序}else if(result 0) {return -1; // 倒序}else {return 0;}}} 3. 结构化数据 调用文件读取工具类返回一个集合集合中的每一条数据即为文件中的一行。 遍历集合取出每一条数据根据制表符\t分割存储到数组中此时数组中下标为0的位置存储的为IP起始地址的字符串下标为1的位置存储的是IP结束地址的字符串下标为2的位置存储的为归属地信息。 将数组中的数据封装为对象后添加到ArrayList集合中此时ArrayList中每一个元素即为一个IP归属地对象。 /*** 结构化数据* param filePath* param encoding* return* throws IOException*/public static ListIPAndLocationPojo getPojoList(String filePath, String encoding) throws IOException{// 获取数据ListString list FileReadUtil.FileRead(filePath, encoding);// 封装结构化后的数据ListIPAndLocationPojo pojoList new ArrayListIPAndLocationPojo();// 逐条遍历取出数据for(String string : list) {// 跳过空行if(string null || string.trim().equals()) {continue;}// 分割字符串String[] strs string.split(\t);// 跳过不合规的数组if(strs.length ! 3) {continue;}String startIP strs[0];String endIP strs[1];String location strs[2];IPAndLocationPojo ipPojo new IPAndLocationPojo(startIP, endIP, location);pojoList.add(ipPojo);}return pojoList; } 4. 集合转数组并排序 由于实体类中存储的起始IP和结束IP为字符串类型默认排序规则不符合要求所以需要提供工具类将实体类中起始IP地址和结束IP地址的字符串转换为long类型为实体类中添加long类型的起始IP和结束IP并实现Comparable接口覆写comparaTo方法来自定义比较规则。 工具类 package com.zh.utils;import java.util.Scanner;/*** Ip地址转换*/ public class IPUtil {/*** ip地址字符串转long类型* * param ipString* return*/public static long ipToLong(String ipString) {String[] str ipString.split(\\.);return (Long.parseLong(str[0]) 24) (Long.parseLong(str[1]) 16) (Long.parseLong(str[2]) 8) Long.parseLong(str[3]);}/*** long 类型数转Ip地址* * param ipLong* return*/public static String longToIP(long ipLong) {StringBuffer sb new StringBuffer();sb.append(String.valueOf(ipLong 24)).append(.).append(String.valueOf((ipLong 0x00ffffff) 16)).append(.).append(String.valueOf((ipLong 0x0000ffff) 8)).append(.).append(String.valueOf(ipLong 0x000000ff));return sb.toString();} } /*** 将对象集合转换为数组并排序* param ipPojoList* return*/public static IPAndLocationPojo[] IpPojoArray(ListIPAndLocationPojo ipPojoList) {// 集合转数组IPAndLocationPojo[] ipPojoArray new IPAndLocationPojo[ipPojoList.size()];ipPojoList.toArray(ipPojoArray);// 数组排序Arrays.sort(ipPojoArray);return ipPojoArray;} 5. 二分法查找 根据用户输入的IP地址采用二分法在已经排序好的数组中查找IP的归属地信息。 由于IP地址库中存储的IP为一段地址范围所以每次判断中间索引的起始地址小于用户输入地址后需要在判断中间索引的结束地址是否大于用户输入IP如果满足条件说明用户输入的IP地址位于当前中间索引起始地址所在的IP地址段直接返回中间索引的IP归属地即为用户的IP归属地 /*** 根据用户输入的地址查寻归属地* param userIp* param arr* return*/public static String searchIP(String userIp, IPAndLocationPojo[] arr) {// 将用户输入的IP转换为long型long userIP IPUtil.ipToLong(userIp);int height arr.length - 1;int low 0;// 二分查找while(low height) {int mid (height low) / 2;// 当中间索引的起始IP大于用户输入IP时结束索引等于中间索引-1if(arr[mid].getStartIPLong() userIP) {height mid - 1;// 当中间索引的结束IP地址大于等于用户输入IP时返回当前归属地信息}else if(arr[mid].getEndIPLong() userIP){ return arr[mid].getLocation();}else {low mid 1;}}return null; } 6. 封装接口类 封装接口对外只提供获取归属地的方法入参:IP地址 出参:归属地 静态代码块中为数组中数据的初始化工作保证每次执行只执行一次不需要重复结构化数据提高效率。文中代码对排序好的数组执行了序列化操作也可以不进行序列化。 序列化工具类: package com.zh.utils;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream;public class FileSerializationUtil {// 序列化对象public static void SerializationObject(String path, Object arr) {try (FileOutputStream fos new FileOutputStream(path);BufferedOutputStream bos new BufferedOutputStream(fos);ObjectOutputStream oos new ObjectOutputStream(bos);) {oos.writeObject(arr);oos.flush();} catch (Exception e) {e.printStackTrace();}}// 对象反序列化public static Object DeserializationObject(String path) {Object obj null;try (FileInputStream fis new FileInputStream(path);BufferedInputStream bis new BufferedInputStream(fis);ObjectInputStream ois new ObjectInputStream(bis);){obj ois.readObject();} catch (Exception e) {e.printStackTrace();}return obj;}} IP地址校验工具类用于校验用户输入IP地址是否符合规范。 package com.zh.utils;import java.util.regex.Matcher; import java.util.regex.Pattern;// IP地址校验 public class IPCheckUtil {// 正则表达式IP地址验证规则private static final String IP_PATTERN ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$;// 创建匹配模式private static final Pattern pattern Pattern.compile(IP_PATTERN);// 匹配IP地址public static boolean checkIP(String ipString) { Matcher matcher pattern.matcher(ipString);return matcher.matches();} }管理类中代码对外提供接口的方法 private static final String IP_ILLEGAL IP地址不合法;private static final String SERIALIZED_FILE_PATH ./serialized_ip_data.ser;private static IPAndLocationPojo[] arr null;// 静态语句块只加载一次static {File serializedPath new File(SERIALIZED_FILE_PATH);if(serializedPath.exists()) {arr (IPAndLocationPojo[])FileSerializationUtil.DeserializationObject(SERIALIZED_FILE_PATH);}else {// 本地IP地址库String filePath ip_location_relation.txt;// 编码方式String encoding UTF-8;ListIPAndLocationPojo list null;try {// 数据结构化list DataProcessManager.getPojoList(filePath, encoding);// 转换为数组并排序arr DataProcessManager.IpPojoArray(list);FileSerializationUtil.SerializationObject(SERIALIZED_FILE_PATH, arr);} catch (IOException e) {e.printStackTrace();}}}/*** 获取IP归属地* param ipString* return* throws IOException*/public static String ipLocationSer(String ipString){// 校验IP地址是否合法if (IPCheckUtil.checkIP(ipString)) {// IP地址合法查询归属地String ipuser DataProcessManager.searchIP(ipString, arr);return ipuser;} else {return IP_ILLEGAL;}} 7. 入口类 充当程序入口执行main方法。 package com.zh.controller;import java.io.IOException; import java.util.List; import java.util.Scanner;import com.zh.manage.DataProcessManager; import com.zh.pojo.IPAndLocationPojo; import com.zh.utils.IPCheckUtil;//入口类 public class SystemController {public static void main(String[] args) {Scanner scanner new Scanner(System.in);System.out.println(请输入要查询的IP地址:);String ip null;while((ip scanner.next())! null) {// long startTime System.currentTimeMillis();String location DataProcessManager.ipLocationSer(ip);System.out.println(location);// System.out.println(耗时: (System.currentTimeMillis() - startTime));System.out.println(请输入要查询的IP地址:);}} }管理类完整代码 package com.zh.manage;import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List;import com.zh.pojo.IPAndLocationPojo; import com.zh.utils.FileReadUtil; import com.zh.utils.FileSerializationUtil; import com.zh.utils.IPCheckUtil; import com.zh.utils.IPUtil;/*** 管理类*/ public class DataProcessManager {private static final String IP_ILLEGAL IP地址不合法;private static final String SERIALIZED_FILE_PATH ./serialized_ip_data.ser;private static IPAndLocationPojo[] arr null;// 静态语句块只加载一次static {File serializedPath new File(SERIALIZED_FILE_PATH);if(serializedPath.exists()) {arr (IPAndLocationPojo[])FileSerializationUtil.DeserializationObject(SERIALIZED_FILE_PATH);}else {// 本地IP地址库String filePath ip_location_relation.txt;// 编码方式String encoding UTF-8;ListIPAndLocationPojo list null;try {// 数据结构化list DataProcessManager.getPojoList(filePath, encoding);// 转换为数组并排序arr DataProcessManager.IpPojoArray(list);FileSerializationUtil.SerializationObject(SERIALIZED_FILE_PATH, arr);} catch (IOException e) {e.printStackTrace();}}}/*** 获取IP归属地* param ipString* return* throws IOException*/public static String ipLocationSer(String ipString){// 校验IP地址是否合法if (IPCheckUtil.checkIP(ipString)) {// IP地址合法查询归属地String ipuser DataProcessManager.searchIP(ipString, arr);return ipuser;} else {return IP_ILLEGAL;}}/*** 结构化数据* param filePath* param encoding* return* throws IOException*/private static ListIPAndLocationPojo getPojoList(String filePath, String encoding) throws IOException{// 获取数据ListString list FileReadUtil.FileRead(filePath, encoding);// 封装结构化后的数据ListIPAndLocationPojo pojoList new ArrayListIPAndLocationPojo();// 逐条遍历取出数据for(String string : list) {// 跳过空行if(string null || string.trim().equals()) {continue;}// 分割字符串String[] strs string.split(\t);// 跳过不合规的数组if(strs.length ! 3) {continue;}String startIP strs[0];String endIP strs[1];String location strs[2];IPAndLocationPojo ipPojo new IPAndLocationPojo(startIP, endIP, location);pojoList.add(ipPojo);}return pojoList; }/*** 将对象集合转换为数组并排序* param ipPojoList* return*/private static IPAndLocationPojo[] IpPojoArray(ListIPAndLocationPojo ipPojoList) {// 集合转数组IPAndLocationPojo[] ipPojoArray new IPAndLocationPojo[ipPojoList.size()];ipPojoList.toArray(ipPojoArray);// 数组排序Arrays.sort(ipPojoArray);return ipPojoArray;}/*** 根据用户输入的地址查寻归属地* param userIp* param arr* return*/private static String searchIP(String userIp, IPAndLocationPojo[] arr) {// 将用户输入的IP转换为long型long userIP IPUtil.ipToLong(userIp);int height arr.length - 1;int low 0;// 二分查找while(low height) {int mid (height low) / 2;// 当中间索引的起始IP大于用户输入IP时结束索引等于中间索引-1if(arr[mid].getStartIPLong() userIP) {height mid - 1;// 当中间索引的结束IP地址大于等于用户输入IP时返回当前归属地信息}else if(arr[mid].getEndIPLong() userIP){ return arr[mid].getLocation();}else {low mid 1;}}return null; }} 运行结果
http://www.pierceye.com/news/68047/

相关文章:

  • 网站app建设图片素材国家信息企业公示系统全国
  • 厦门网站制作建设h5一般收费标准
  • 苏宁网站优化与推广毕业设计代做网站多少钱
  • 做律师事务所网站自己做的网站 怎么放大文件
  • 临漳手机网站建设推广软文代写
  • 淮南市城乡建设局网站wordpress5.1好用
  • 广告多的网站石家庄新钥匙网站建设
  • 招商网站大全免费网站公司排行榜
  • 网站里的活动专题栏怎么做上海做网站建设的公司
  • 自身网站的建设和推广力度不足儿童 网站 设计欣赏
  • 无锡朝阳网站推广wordpress主题页添加
  • 关于网站建设的案例分析建设网站的3个必要条件
  • 中山网站建设公司哪个好wordpress活动链接
  • 道路建设去什么网站能看到网站地图提交
  • phpcms做汽车网站做二手车放在哪个网站好
  • 企业作风建设包括哪些方面长沙seo优化外包公司
  • 湖北省建设厅七大员报名网站做视频网站怎么看不会卡
  • 哈尔滨做网站企业wordpress知名中国网站
  • wordpress站点错误页面模板这样选
  • 建设项目网站网站程序和空间区别
  • 个人的网站怎么备案河源网站推广
  • 西宁建设厅培训中心网站网页界面设计的参考文献
  • 做我女朋网站源码北京高端it网站建设
  • 个人主页网站设计论文全自动引流推广软件免费
  • 织梦怎么做单页网站建网站公司营销型网站建设
  • 网站制作动态转静态怎么做如何看出网站用dede做的
  • 用电脑建立网站logo制作网站免费
  • 合肥网站建设培训机构网络营销指导如何做
  • 郑州哪家建设网站佛山外贸网站
  • 做ppt图片用的网站有哪些网站查询seo