【已解决】ip2region解析ip获取地区位置 在linux部署出现java文件操作报错:java.io.FileNotFoundException

1、依赖

java 复制代码
        <dependency>
            <groupId>org.lionsoul</groupId>
            <artifactId>ip2region</artifactId>
            <version>2.7.0</version>
        </dependency>

2.加入ip2region.xdb文件

ip2Region下载地址

3.加入到项目里面去

java 复制代码
把 ip2region.xdb 文件放到 resources 文件夹里面的去

在Maven里面添加依赖,防止把这个xdb文件编译:

java 复制代码
<plugin>
  <artifactId>maven-resources-plugin</artifactId>
   <configuration>
    <nonFilteredFileExtensions>
         <nonFilteredFileExtension>xdb</nonFilteredFileExtension>
       </nonFilteredFileExtensions>
     </configuration>
</plugin>

4.工具类

java 复制代码
	/**
	 * 通过ip 获取ip所在地址
	 * @param ip
	 * @return
	 */
	public static String getIpAddress(String ip){
		if ("127.0.0.1".equals(ip) || ip.startsWith("192.168")) {
			return "局域网 ip";
		}
		//双重校验锁方式加载对象实例(懒汉式)
		if (searcher == null) {
			synchronized (HttpServletRequestUtils.class) {
				if (searcher == null) {
					try {
						Resource resource = new ClassPathResource("ip2region.xdb");
						HttpServletRequestUtils httpServletRequestUtils = new HttpServletRequestUtils();
						String filePath = httpServletRequestUtils.getClass().getClassLoader().getResource("ip2region.xdb").getFile();
						File file= new File(filePath);
						log.info("文件路径:{}是否存在:{}:", filePath, file.exists());
						// 通过流将文件复制到file中
						FileUtils.writeFile(resource.getInputStream(), filePath);
						
						//下面直接获取可能会导致 java.io.FileNotFoundException 异常
//						File file = ResourceUtils.getFile("classpath:ipdb/ip2region.xdb");
						String dbPath = file.getPath();
						searcher = Searcher.newWithFileOnly(dbPath);
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		String region = null;
		String errorMessage = null;
		try {
			region = searcher.search(ip);
		} catch (Exception e) {
			errorMessage = e.getMessage();
			if (errorMessage != null && errorMessage.length() > 256) {
				errorMessage = errorMessage.substring(0,256);
			}
			e.printStackTrace();
		}
		// 输出 region
		return region;
	}
	/*
	 * 读取返回结果
	 */
	private static String read(InputStream is) throws IOException {
		StringBuffer sb = new StringBuffer();
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String line = null;
		while ((line = br.readLine()) != null) {
			line = new String(line.getBytes(), "utf-8");
			sb.append(line);
		}
		br.close();
		return sb.toString();
	}

5. 注意事项:

1.部署到线上之后:出现java文件操作报错:java.io.FileNotFoundException

java 复制代码
java.io.FileNotFoundException: class path resource [ip2region.xdb] 
cannot be resolved to absolute file path because it does not reside
 in the file system: 
相关推荐
是真的小外套2 小时前
2023年软考中级信息安全工程师|选择题考点+5道案例分析真题解析(含答案)
java·服务器·网络
会编程的吕洞宾2 小时前
Spring AI 2.0 结构化输出实战:让大模型返回精准 JSON 不再“抽风“,准确率从 78% 拉到 99%
java·后端
项目題供诗2 小时前
ai大模型(三)
java
wangjialelele2 小时前
一文梳理 Spring Cloud 核心组件:Eureka、Nacos、OpenFeign、LoadBalancer 与 Gateway
java·spring boot·spring·spring cloud·微服务·nacos
用户6222884048192 小时前
站在JDK的角度理解线程池:从execute 到 getTask 一篇讲透
java·面试
Wang's Blog2 小时前
Java 项目实战: 交友项目 - 个人资料管理的需求分析与接口设计
java·开发语言·交友
北风toto2 小时前
Linux 服务器如何判断 CPU 是否支持 x86-64-v2 指令集
java·linux·服务器
程序员-Benothing2 小时前
Linux抓包分析教程:tcpdump、Wireshark、tshark实战
linux·运维·服务器
萧瑟余晖2 小时前
Spring Security综合实战与安全加固详解
java·安全·spring
QMCY_jason2 小时前
ubuntu x86_64架构的编译 aarch64架构的程序
linux·运维·ubuntu