【已解决】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: 
相关推荐
寻星探路16 小时前
Java EE初阶启程记12---synchronized 原理
java·java-ee
qq_5746562516 小时前
java代码随想录day50|图论理论基础
java·算法·leetcode·图论
蒋星熠16 小时前
Maven项目管理与构建自动化完全指南
java·前端·python·自动化·maven
sheji341616 小时前
【开题答辩全过程】以 ssm框架的智能校园服务系统为例,包含答辩的问题和答案
java·eclipse
做运维的阿瑞17 小时前
GPU即服务:Linux与云原生如何联手开启AI算力“自来水“时代
linux·人工智能·云原生
青云交17 小时前
Java 大视界 -- 基于 Java 的大数据实时流处理在工业物联网设备故障预测与智能运维中的应用
java·flink·kafka·工业物联网·设备故障预测·智能运维·实时流处理
fat house cat_17 小时前
为什么RocketMQ选择mmap+write?RocketMQ零拷贝技术深度解析
java·rocketmq·零拷贝
骄傲的心别枯萎17 小时前
项目1:FFMPEG推流器讲解(一):FFMPEG重要结构体讲解
linux·ffmpeg·音视频·视频编解码·rv1126
报错小能手18 小时前
linux学习笔记(16)进程间通信——管道
linux·笔记·学习
月疯18 小时前
FLASK与JAVA的文件互传并带参数以及流上传(单文件互传亲测)
java·python·flask