代码
Resource resource = new PathMatchingResourcePatternResolver().getResource("data/apiclient_cert.p12");
File file = resource.getFile();
我们idea中运行服务时,可以正常执行。但是将服务打包成可执行jar的包,以jar服务运行服务时,抛出以下错误:
cannot be resolved to absolute file path because it does not reside in the file system:
出现以上问题的原因:
打包后Spring无法使用resource.getFile()访问JAR中的路径的文件,必须使用resource.getInputStream()。
得到InputStream才行
InputStream.available()得到InputStream的大小(注意这个方法最大值为int,当文件大小超过int时,就会不准确)