Resource resource = new ClassPathResource("data/test/txt"); String path = resource.getFile().getPath());
1 2
Caused by: java.io.FileNotFoundException: class path resource [application.yml] cannot be resolved to absolute file path because it does not reside in the file system:
new ClassPathResource方法获取相对路径是没问题的,这个报错其实是getFile方法报出的
原因是在jar里,返回的是一个Jar协议地址:jar:file:/xxx/xx.jar!/xxxx
getFile源码:
1 2 3 4 5 6 7 8 9 10 11 12
publicstatic File getFile(URL resourceUrl, String description)throws FileNotFoundException { Assert.notNull(resourceUrl, "Resource URL must not be null"); if (!"file".equals(resourceUrl.getProtocol())) { thrownew FileNotFoundException(description + " cannot be resolved to absolute file path because it does not reside in the file system: " + resourceUrl); } else { try { returnnew File(toURI(resourceUrl).getSchemeSpecificPart()); } catch (URISyntaxException var3) { returnnew File(resourceUrl.getFile()); } } }