【已解决】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: 
相关推荐
10km10 分钟前
java:Apache Commons Configuration2占位符解析异常的正确解法:${prefix:name:-default}
java·apache·configuration2·变量插值·interpolation
customer0810 分钟前
【开源免费】基于SpringBoot+Vue.JS个人博客系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
灰色人生qwer18 分钟前
SpringBoot 项目配置日志输出
java·spring boot·后端
2301_7930698227 分钟前
Spring Boot +SQL项目优化策略,GraphQL和SQL 区别,Spring JDBC 等原理辨析(万字长文+代码)
java·数据库·spring boot·sql·jdbc·orm
阿华的代码王国34 分钟前
【从0做项目】Java搜索引擎(6)& 正则表达式鲨疯了&优化正文解析
java·后端·搜索引擎·正则表达式·java项目·从0到1做项目
服务端相声演员34 分钟前
Oracle JDK、Open JDK zulu下载地址
java·开发语言
YH_DevJourney34 分钟前
Linux-C/C++《C/8、系统信息与系统资源》
linux·c语言·c++
是姜姜啊!35 分钟前
java连接redis
java·redis
hhw19911236 分钟前
spring boot知识点5
java·数据库·spring boot
忆源38 分钟前
SOME/IP--协议英文原文讲解11
网络·网络协议·tcp/ip