前言:
写了一个工具类,要读取classpath下的文件,使用
Resource resource = new ClassPathResource(filePath);
File file = resource.getFile();
在本地测试,没发现问题,但是将项目打包成jar包后运行,发现报错
Caused by: java.io.FileNotFoundException: class path resource [test.txt] cannot be resolved to absolute file path because it does not reside in the file system: j
原因:
打包后Spring试图访问文件系统路径,但无法访问JAR中的路径。
解决:
Resource resource = new ClassPathResource(filePath);
InputStream inputStream = resource.getInputStream();