SpringBoot项目打包部署,读取jar里面的文件报错500,异常日志关键提示
cannot be resolved to absolute file path because it does not reside in the file system
报错定位
is = new FileInputStream(ResourceUtils.getFile("classpath:static/data_template/biz/export_XXXX_report.xlsx"));
从报错可看到读取文件路径****XXX.jar!******.xlsx,XXX.jar就是打成jar,里面包含了需要读取的模板文件
具体Spring core(版本5.2.12)包源码ResourceUtils
public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
Assert.notNull(resourceUrl, "Resource URL must not be null");
if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
//报错所在地方
throw new FileNotFoundException(
description + " cannot be resolved to absolute file path " +
"because it does not reside in the file system: " + resourceUrl);
}
try {
return new File(toURI(resourceUrl).getSchemeSpecificPart());
}