Springboot项目获取resources下的模板文件,本地测试使用ClassPathResource都可以正常读取,但打包成jar包传到服务器上就无法获取 ,错误信息如下:
cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/xx/xx.jar!/BOOT-INF/classes!/xx
之前的代码如下:
Resource resource = new ClassPathResource(filePath); File file = resource.getFile();
使用下面方式读取即可解决:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("xxx/xxx.ftl"); FileUtils.copyInputStreamToFile(Objects.requireNonNull(inputStream, "文件找不到"), file);