Spring Boot 读取静态资源文件

本文介绍了如何在Spring项目中读取位于类路径下的资源文件,包括文件系统和JAR包内的处理方式,并提供了具体的代码示例。

一、需求场景

有时候我们需要在项目中使用一些静态资源文件,比如城市信息文件 countries.xml,在项目启动后读取其中的数据并初始化写进数据库中。

二、实现

静态资源文件 countries.xml 放在 src/main/resources 目录下

使用 Spring 的 ClassPathResource 来实现 :

Resource resource = new ClassPathResource("countries.xml");
File file = resource.getFile();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
 
  • 1
  • 2
  • 3

ClassPathResource 类的注释如下:

Resource implementation for class path resources. Uses either a given ClassLoader or a given Class for loading resources.

Supports resolution as java.io.File if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL.

翻译过来就是:

类路径资源的资源实现。使用给定的ClassLoader或给定的类来加载资源。

如果类路径资源驻留在文件系统中,则支持解析为 java.io.File,如果是JAR中的资源则不支持。始终支持解析为URL

三、Jar 中资源文件

上面也提到了,如果静态资源文件在文件系统里,则支持解析为 java.io.File,程序是能正常工作的。

到项目打包成 Jar 包放到服务器上运行就报找不到资源的错误了!

解决法案是:不获取 java.io.File 对象,而是直接获取输入流

Resource resource = new ClassPathResource("countries.xml");
BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
 
  • 1
  • 2

说明:构造得到 resource 对象之后直接获取其输入流 resource.getInputStream()

---------------------------------------补充-----------------------------------------

resource.getFile() expects the resource itself to be available on the file system, i.e. it can't be nested inside a jar file. This is why it works when you run your application in STS but doesn't work once you've built your application and run it from the executable jar. Rather than using getFile() to access the resource's contents, I'd recommend using getInputStream() instead. That'll allow you to read the resource's content regardless of where it's located.

If you're using Spring framework then reading ClassPathResource into a String is pretty simple using Spring framework's FileCopyUtils:

String data = "";
ClassPathResource cpr = new ClassPathResource("static/file.txt");
try {
    byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream());
    data = new String(bdata, StandardCharsets.UTF_8);
} catch (IOException e) {
    LOG.warn("IOException", e);
}


资源下载链接为: https://pan.quark.cn/s/f989b9092fc5 在Java项目开发中,Spring Boot框架被广泛应用于Web应用程序的构建。随着项目规模的不断扩大,配置文件、模板文件等资源文件的管理逐渐变得复杂起来,因此掌握如何读取resources目录下的文件显得尤为重要。本文将深入探讨Spring Boot读取resources目录文件的两种常见方法,并通过测试用例来加深理解。 资源文件在Java项目中扮演着关键角色,它们通常用于存储配置文件、模板文件、图片等静态资源。这些文件能够被应用程序调用,以实现各种功能。例如,当需要生成PDF文件时,模板文件就用于确定PDF的格式和内容。按照Maven的惯例,资源文件一般存放在项目的src/main/resources目录中。比如,合同协议PDF模板就可以存放在resources/template/test.pdf路径下。 ClassPathResource是Spring提供的一个类,用于读取resources目录下的文件。以下是示例代码: 在上述代码中,我们首先创建了一个ClassPathResource对象,并将其初始化为指定的资源文件路径。接着,通过调用getInputStream()方法,将Resource对象转换为InputStream对象,从而能够读取文件内容。 另一种读取resources目录文件的方法是使用getContextClassLoader().getResourceAsStream()。示例代码如下: 这里,我们借助getContextClassLoader().getResourceAsStream()方法来读取资源文件,并将其转换为InputStream对象,以便进行文件内容的读取。 下面是一个测试用例,用于展示上述两种读取方法: 在该测试用例中,我们分别运用了两种方
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值