【已解决】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: 
相关推荐
码农飞哥8 分钟前
互联网大厂Java面试实战:Spring Boot到微服务的技术问答解析
java·数据库·spring boot·缓存·微服务·消息队列·面试技巧
愚戏师9 分钟前
Linux复习笔记(三) 网络服务配置(web)
linux·运维·笔记
liudongyang12310 分钟前
jenkins 启动报错
java·运维·jenkins
曹牧17 分钟前
JSON 实体属性映射的最佳实践
java
qq_5432485228 分钟前
Tomcat服务部署
java·tomcat
JANYI201832 分钟前
嵌入式MCU和Linux开发哪个好?
linux·单片机·嵌入式硬件
Auc241 小时前
OJ判题系统第4期之判题机模块架构——设计思路、实现步骤、代码实现(工厂模式、代理模式的实践)
java·spring cloud·log4j·mybatis·代理模式·工厂模式
熊大如如1 小时前
Java NIO 文件处理接口
java·linux·nio
q567315231 小时前
Go语言多线程爬虫与代理IP反爬
开发语言·爬虫·tcp/ip·golang
晚秋大魔王1 小时前
OpenHarmony 开源鸿蒙南向开发——linux下使用make交叉编译第三方库——nettle库
linux·开源·harmonyos