获取打包后jar包内resource文件路径
本地环境获取resource下资源文件正常,打包到容器中获取文件的路径会出现下面的异常
java.lang.RuntimeException: java.io.FileNotFoundException: class path resource [xxx/xxx] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/xxx.jar!/BOOT-INF/classes!/xxx/xxx
解决办法
/**
* 获取resource下文件路径
*
* @param resourceSrc 原文件resource下路径
* @param fileName 文件名称
* @return
*/
public static String getFilePath(String resourceSrc, String fileName) {
File file = new File(fileName);
if (!file.exists()) {
ClassPathResource resource = new ClassPathResource(resourceSrc);
try {
FileUtil.writeFromStream(resource.getInputStream(), new File(fileName));
} catch (IOException e) {
log.error("获取证书失败");
e.printStackTrace();
}
}
return file.getAbsolutePath();
}