springboot下getResource无法访问打包之后的资源文件夹下的文件

在Spring Boot应用中,当从jar包内访问资源文件时,使用`getResource`会遇到问题。文章详细描述了这个问题,即在IDEA中运行正常,但打包成jar后无法访问。原因在于java.io文件句柄不支持直接访问jar包内的压缩内容。为解决此问题,提出了三种解决方案,包括使用`getResourceAsStream`方法。经验证,这些解决方案在IDEA和打包后的jar中都能正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

代码
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
idea启动没问题,能正常访问到文件,但是打包后就无法访问到了
在这里插入图片描述

2020-12-03 22:22:33.027  INFO 1764 --- [           main] com.jy.DesignApplication                 : Starting DesignApplication v0.0.1-SNAPSHOT on E8BP5A2FE9DHB6I with PID 1764 (C:\Users\Administrator\Desktop\design-0.0.1-SNAPSHOT.jar started by Administrator in C:\Users\Administrator\Desktop)
2020-12-03 22:22:33.030  INFO 1764 --- [           main] com.jy.DesignApplication                 : No active profile set, falling back to default profiles: default
java.io.FileNotFoundException: file:\C:\Users\Administrator\Desktop\design-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes!\myConfig.properties (文件名、目录名或卷标语法不正确。)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at com.jy.config.MyAoutoConfig.getPerson(MyAoutoConfig.java:17)
        at com.jy.config.MyAoutoConfig$$EnhancerBySpringCGLIB$$3d8b12ee.CGLIB$getPerson$0(<generated>)
        at com.jy.config.MyAoutoConfig$$EnhancerBySpringCGLIB$$3d8b12ee$$FastClassBySpringCGLIB$$cd9d1717.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
        at com.jy.config.MyAoutoConfig$$EnhancerBySpringCGLIB$$3d8b12ee.getPerson(<generated>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)

原因分析:

idea本地访问的时候,他是以正常文件目录来访问的,但是经过打成jar包之后,整个文件打包成了一个压缩包,java.io的文件句柄是无法直接访问压缩文件的内部的内容的

解决方案:

java.io文件句柄无法访问绝对路径的压缩文件,getResource方法返回值是个Url,所以解决方案有三种:

this.getClass().getResource("/myConfig.properties").openStream()
this.getClass().getClassLoader().getResource("myConfig.properties").openStream()
this.getClass().getClassLoader().getResourceAsStream("myConfig.properties")

下面是通过getResourceAsStream方式获取的解决方案的结果
在这里插入图片描述
在这里插入图片描述
替换之后idea本地正常启动
在这里插入图片描述
替换之后打包也正常启动

Spring Boot 项目中,您可以使用 `ClassPathResource` 类来获取 `resources` 文件夹下的文件。`ClassPathResource` 是 Spring 框架提供的用于加载类路径下资源的类。 以下是一个示例,展示如何使用 `ClassPathResource` 获取 `resources` 文件夹下的文件: ```java import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import java.io.File; import java.io.IOException; public class ResourceExample { public static void main(String[] args) { String fileName = "myfile.txt"; try { // 使用 ClassPathResource 获取 resources 文件夹下的文件 Resource resource = new ClassPathResource(fileName); File file = resource.getFile(); String absolutePath = file.getAbsolutePath(); System.out.println("Absolute Path: " + absolutePath); } catch (IOException e) { // 异常处理 } } } ``` 在上面的示例中,我们通过创建 `ClassPathResource` 对象来加载类路径下的资源。在构造函数中,我们传入了文件名 `myfile.txt`。 然后,通过 `getFile()` 方法获取资源文件对应的 `File` 对象,再通过 `getAbsolutePath()` 方法获取文件的绝对路径。 请注意,使用 `getFile()` 方法需要注意两点: 1. 如果资源文件打包JAR 文件,`getFile()` 方法将无法直接获取到文件对象,而会抛出 `UnsupportedOperationException` 异常。此时,您可以尝试使用其他方法来读取资源文件内容,例如使用 `InputStream`。 2. 如果资源文件不存在或无法访问,`getFile()` 方法也会抛出 `FileNotFoundException` 异常。在处理异常时,请确保对应的错误处理逻辑。 希望这可以帮助到您!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值