java根据ip创建文件夹_新报: 根据IP获取物理地址不需要调用接口啦

一、 复制data文件夹到自己的项目当中

可以进行加载到这个文件即可 博主放入在项目的根目录当中

20200710205020692278.png

带入maven依赖 因为这个编辑器的原因 复制依赖 显示不出来淦!!!! 图片自己打吧

org.lionsoul

ip2region

1.7.2

20200710205020854377.png

二、最重要的一步 编译地理位置

找到自己的项目地址

执行打开cmd 执行这段代码

java -Dfile.encoding=utf-8 -jar dbMaker-1.2.2.jar -src ./data/ip.merge.txt -region ./data/global_region.csv

java -Dfile.encoding=utf-8 -jar dbMaker-1.2.2.jar -src ./data/ip.merge.txt -region ./data/global_region.csv

20200710205020973510.png

20200710205021036006.png

请跟着一步步来

20200710205021089713.png

20200710205021170763.png

代码开始 创建iputils

package top.yangbuyi.system.webapi;

import org.lionsoul.ip2region.DataBlock;

import org.lionsoul.ip2region.DbConfig;

import org.lionsoul.ip2region.DbSearcher;

import org.lionsoul.ip2region.Util;

import java.io.File;

import java.io.UnsupportedEncodingException;

import java.lang.reflect.Method;

import java.net.URLDecoder;

/**

* description: 杨不易网站 :www.yangbuyi.top

* program: yangbuyi-erp-2020

* ClassName: iputils

* create: 2020-07-10 09:20

*

* @author: yangbuyi

* @since: JDK1.8

* @iputils:

**/

/**

* 具体请看 SQL当中的 data文件 和jar包

* 通过下面语句进行 编译 地理位置

* java -jar dbMaker-1.2.2.jar -src ./data/ip.merge.txt -region ./data/global_region.csv

*/

public class iputils {

public static String getCityInfo(String ip) throws UnsupportedEncodingException {

//db

String dbPath = new File("SQL/data/ip2region.db").getPath();

dbPath = URLDecoder.decode(dbPath, "UTF-8");

File file = new File(dbPath);

if (file.exists() == false) {

System.out.println("Error: Invalid ip2region.db file");

}

//查询算法

int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree

//DbSearcher.BINARY_ALGORITHM //Binary

//DbSearcher.MEMORY_ALGORITYM //Memory

try {

DbConfig config = new DbConfig();

DbSearcher searcher = new DbSearcher(config, dbPath);

//define the method

Method method = null;

switch (algorithm) {

case DbSearcher.BTREE_ALGORITHM:

method = searcher.getClass().getMethod("btreeSearch", String.class);

break;

case DbSearcher.BINARY_ALGORITHM:

method = searcher.getClass().getMethod("binarySearch", String.class);

break;

case DbSearcher.MEMORY_ALGORITYM:

method = searcher.getClass().getMethod("memorySearch", String.class);

break;

}

DataBlock dataBlock = null;

if (Util.isIpAddress(ip) == false) {

System.out.println("Error: Invalid ip address");

}

dataBlock = (DataBlock) method.invoke(searcher, ip);

String decode = URLDecoder.decode(dataBlock.getRegion(), "UTF-8");

return decode;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

public static void main(String[] args) throws Exception {

System.err.println(iputils.getCityInfo("106.18.167.102"));

}

}

最后 测试代码

20200710205021265483.png

太累了吧!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值