springboot使用jar包方式启动,找不到resources目录中的配置文件(运行时)FileNotFoundException
阅读原文时间:2023年07月11日阅读:1

将springboot项目打包成jar包,使用 java -jar jar包进行启动,富文本框使用ckeditor+ckfinder;

因为ckfinder自定义配置文件了,上传图片时出现了异常

DefaultResourceLoader loader = new DefaultResourceLoader();
String absolutePath = loader.getResource(this.xmlFilePath).getFile().getAbsolutePath();

抛出异常:

java.io.FileNotFoundException: class path resource [ckfinder-config.xml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/E:/Development/VCS/GIT/dmahzForum/dmahzForum/target/dmahz-forum-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/ckfinder-config.xml
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:131)
at cn.dmahz.config.CKFinderConfiguration.checkIfReloadConfig(CKFinderConfiguration.java:197)
at com.ckfinder.connector.configuration.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:59)
at com.ckfinder.connector.configuration.ConfigurationPathBuilder.getBaseDir(ConfigurationPathBuilder.java:55)
at com.ckfinder.connector.configuration.ConfigurationFactory.getBaseFolder(ConfigurationFactory.java:189)
at com.ckfinder.connector.configuration.ConfigurationFactory.updateResourceTypesPaths(ConfigurationFactory.java:151)

,测试了好几种方法,结果是只能通过getInputStream()流的方式获取文件内容,否则就会抛出上面所述的异常;;;

更改之后如下:

ClassPathResource classPathResource = new ClassPathResource("ckfinder-config.xml");
String path1 = classPathResource.getPath();
InputStream inputStream = classPathResource.getInputStream();
System.err.println("path1:"+path1);
System.err.println("inputStream:"+inputStream+",Size:"+inputStream.available());

证明可以运行没有问题

详细说明:https://blog.csdn.net/u013467442/article/details/88807557