springboot打包为jar后获取resources文件夹下的文件
在项目打包为jar之后,通过绝对路径是无法获取到文件的,反而会报错,所以只能通过流的方式,先拿到文件内容,然后再转换为文件
ClassPathResource classPathResource = new ClassPathResource("exceltmp/template_export.xls"");
InputStream inputStream = classPathResource.getInputStream();
//生成目标文件
File somethingFile = File.createTempFile("template_export_copy", ".xls");
try {
FileUtils.copyInputStreamToFile(inputStream, somethingFile);
} finally {
IOUtils.closeQuietly(inputStream);
}
本文介绍在SpringBoot项目中,如何在项目被打包成jar后正确读取resources文件夹下的文件。通过使用ClassPathResource和InputStream,可以避免绝对路径读取的局限,确保文件内容的正确读取和临时文件的生成。
2698

被折叠的 条评论
为什么被折叠?



