项目在本地可以获取到classpath下的文件,但是打包发布到服务器(Linux)上后,就会报错cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/srv/project.jar!/BOOT-INF/classes!/static/json/14.json
resource.getFile()在本地上测试是没问题的,因为是直接运行的SpringBoot项目,但是打成jar包之后就会报错cannot be resolved to absolute file path...。跟到源码org.springframework.util.ResourceUtils#getFile(java.net.URL, java.lang.String)中之后,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/** URL protocol for a file in the file system: "file". */ publicstaticfinal String URL_PROTOCOL_FILE = "file";
publicstatic File getFile(URL resourceUrl, String description)throws FileNotFoundException { Assert.notNull(resourceUrl, "Resource URL must not be null"); if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) { thrownew FileNotFoundException( description + " cannot be resolved to absolute file path " + "because it does not reside in the file system: " + resourceUrl); } try { returnnew File(toURI(resourceUrl).getSchemeSpecificPart()); } catch (URISyntaxException ex) { // Fallback for URLs that are not valid URIs (should hardly ever happen). returnnew File(resourceUrl.getFile()); } }