---
url: /diary/51pz0z8h/index.md
description: Spring Boot 中获取 resources 目录下资源文件的多种方法，解决 JAR 包部署后无法读取文件的问题。
---
> Jar包获取resource下的资源

举例：获取 region.json 文件

::: file-tree

* src
  * main
    * java
      * ...
    * resources
      * region
        * region.json
      * ...
* ...
  :::

```java
URL url = ResourceUtils.getURL("classpath:region/region.json");
String regionJson;  
try (InputStream inputStream = url.openStream()) {  
    byte[] bytes = inputStream.readAllBytes();  
    regionJson = new String(bytes, StandardCharsets.UTF_8);  
}
```

推荐使用工具简化，例如 [hutool](https://hutool.cn/)。
