SpringBoot打成Jar包获取resource下文件的问题

在工作中遇到的问题,在idea中读取resource下的template文件夹下的模板文件时正常,但是打成Jar包之后在调用的时间读取报错File Not found exception

下面是resource的目录结构,框选的是要读取的文件

在这里插入图片描述

在本地IDEA中读取正常,打成Jar包后就无法访问的

一开始的写法是下面这种:
ResourceUtils是 org.springframework.util.ResourceUtils包

//文件位置
String path = "classpath:template/双公示模板新.xlsx";
//使用ResourceUtils获取文件
File file = ResourceUtils.getFile(path);
//获取文件流
FileInputStream fileInputStream = new FileInputStream(file);

这种方式在本地测试是没有问题的,但是打成jar包之后不行。开始以为是资源文件过滤的问题,就在pom文件里加了以下配置

当然,这段配置是必须加的

<build>
        <!--打包的jar去掉版本号 默认值是${artifactId}-${version}-->
        <finalName>${artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.7.RELEASE</version>
            </plugin>
        </plugins>

 		<!--资源过滤配置-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!--是否替换资源中的属性-->
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.yml</include>
                    <include>**/Dockerfile</include>
                    <include>**/*.xlsx</include>
                </includes>
                <!--是否替换资源中的属性-->
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

加完配置之后还是不行。

ClassPathResource类获取流

使用以下这种方式才是可以的:

这种方式才能在打成jar包之后读取到文件,具体的原因我这边不清楚,所以麻烦知道的在评论区留言,非常感谢

ClassPathResource classPathResource = new ClassPathResource(path);
InputStream pathResourceInputStream = classPathResource.getInputStream();
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot中,可以使用`ResourceLoader`来读取`resources`目录中的文件。下面是一个示例代码,演示如何使用`ResourceLoader`读取资源文件: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Component; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @Component public class ResourceReader { private final ResourceLoader resourceLoader; @Autowired public ResourceReader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public String readResourceFile(String filePath) throws IOException { Resource resource = resourceLoader.getResource("classpath:" + filePath); Path path = resource.getFile().toPath(); byte[] bytes = Files.readAllBytes(path); return new String(bytes, StandardCharsets.UTF_8); } } ``` 在上述代码中,我们创建了一个名为`ResourceReader`的组件,并通过构造函数注入了`ResourceLoader`。`readResourceFile`方法接受一个文件路径作为参数,可以是相对于`resources`目录的相对路径或者绝对路径。 在`readResourceFile`方法中,我们首先使用`resourceLoader.getResource("classpath:" + filePath)`获取资源文件的`Resource`对象。然后,我们通过调用`getFile().toPath()`将其转换为`Path`对象。最后,我们使用`Files.readAllBytes(path)`读取文件的所有字节,并将其转换为字符串返回。 需要注意的是,这里的示例代码假设资源文件存在于Spring Boot应用程序的类路径下的`resources`目录中。如果文件不存在或路径不正确,将会抛出`IOException`。因此,建议在实际使用时进行适当的错误处理。 请确保将`ResourceReader`组件注册到Spring Boot应用程序的上下文中,以便可以使用`@Autowired`注入。然后,您可以在其他组件或服务中使用`ResourceReader`来读取资源文件
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值