解决springboot项目找不到resources目录下的资源问题
springboot项目找不到resources目录下的资源
问题描述:
将老的mvc项目转为boot后找不到resources文件夹下的资源文件
原因:
war包采用的是tomcat部署,tomcat会对war包进行解压,以及目录的一些操作。而springboot使用jar包部署,服务器中是不存在相关目录的。
环境:
springboot 2.2.2RELAESE
主要的API:
ClassPathResource classPathResource = new ClassPathResource(filePath); InputStream inputStream = classPathResource.getInputStream();
工具类
import java.io.File;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
public class FileUtil {
public File getResourceFile(String filePath) throws Exception{
try {
ClassPathResource classPathResource = new ClassPathResource(filePath);
InputStream inputStream = classPathResource.getInputStream();
//生成目标文件
File somethingFile = File.createTempFile("DailyReportTemplate", ".xls");
try {
FileUtils.copyInputStreamToFile(inputStream, somethingFile);
} finally {
IOUtils.closeQuietly(inputStream);
}
return somethingFile;
} catch (Exception e) {
throw new Exception(e);
}
}
}
运行jar文件时,ClassPathResource无法读取到资源文件的问题
问题场景:
在idea中运行,一切正常,资源文件都可以访问到,但打成jar包后,使用java -jar的形式去启动,就访问不到resource下的资源文件了
网上搜了很多文章,但试了后都不好使
我的路径是配置在properties文件中,然后读取配置文件中的值,然后拼接文件路径,再使用ClassPathResource去读取的
开始时我配置文件中是这样写的:
#路径中注意首尾不要有空格 service.config-root=static/service/ service.config-name=AppConfig.json
程序代码读出后拼接:
@Value("${service.config-root}")
private String configRoot;
@Value("${service.config-name}")
private String configName;
.....省略无关code.....
public String getPath(){
String configPath = this.configRoot + this.configName;
return configPath;
}
但运行jar后,直接FileNotFoundException了
解决:
方案1:
主要是斜杠"\"和反斜杠"/"的问题,配置文件修改如下:
#路径中注意首尾不要有空格 service.config-root=static\\tileservice\\ service.config-name=AppConfig.json
方案2:
使用"File.spearator"拼接路径
service.config-root=static
service.config-name=AppConfig.json
@Value("${service.config-root}")
private String configRoot;
@Value("${service.config-name}")
private String configName;
.....省略无关code.....
public static <T> T readJsonFromClassPath(Type type) throws IOException {
//这里使用File.spearator拼接
ClassPathResource resource = new ClassPathResource(this.configRoot + File.spearator + this.configName);
if (resource.exists()) {
return JSON.parseObject(resource.getInputStream(), StandardCharsets.UTF_8, type, .....
}
}
搞定!
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
- Springboot 项目读取Resources目录下的文件(推荐)
- 解决@springboottest注解无法加载src/main/resources目录下文件
- springboot项目读取resources目录下的文件的9种方式
- springboot实现jar运行复制resources文件到指定的目录(思路详解)
- SpringBoot中读取jar包中的resources目录下的文件的三种方式
- SpringBoot如何读取resources目录下的文件
- SpringBoot实现本地上传文件到resources目录
- Springboot获取jar包中resources资源目录下的文件
- Springboot项目启动不加载resources目录下的文件问题
- SpringBoot下获取resources目录下文件的常用方法
相关文章
解决在Idea 2020.2下使用 Lombok的注解不生效的问题(插件安装了,依赖也写了,自动注解也设置了)
这篇文章主要介绍了在Idea 2020.2下使用 Lombok的注解不生效的问题(插件安装了,依赖也写了,自动注解也设置了),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-08-08
spring 和 idea 建议不要使用 @Autowired注解的原因解析
@Autowired 是Spring框架的注解,而@Resource是JavaEE的注解,这篇文章主要介绍了spring和idea建议不要使用@Autowired注解的相关知识,需要的朋友可以参考下2023-11-11
Java高性能实体类转换工具MapStruct的使用教程详解
MapStruct 是一个代码生成器,它基于约定优于配置的方法,极大地简化了 Java bean 类型之间的映射实现,本文主要介绍了MapStruct的具体使用以及如何进行实体类转换,感兴趣的可以了解下2024-03-03


最新评论