问题备忘: class path resource [xx] cannot be resolved to absolute file path because it does not reside
问题描述
测试服务的版本是Spring Cloud Dalston.SR5
在Spring Boot中配置https时,代码如下:
@Bean@ConditionalOnExpression("#{ ${self.https.enable:false}}")public EmbeddedServletContainerFactory servletContainer() {TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();tomcat.addAdditionalTomcatConnectors(createSslConnector());return tomcat;}private Connector createSslConnector() {Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();ClassPathResource resource = new ClassPathResource(".keystore");try {connector.setScheme("https");connector.setSecure(true);connector.setPort(port);protocol.setSSLEnabled(true);protocol.setKeystoreFile(resource.getFile().getAbsolutePath());protocol.setKeystorePass(keystorePass);protocol.setKeyAlias(keyAlias);protocol.setKeyPass(keyPass);} catch (IOException e) {e.printStackTrace();}return connector;}
以上代码在我们idea中运行服务时,可以正常执行。但是将服务打包成可执行jar的包,以jar服务运行服务时,抛出以下错误:
java.io.FileNotFoundException: class path resource [.keystore] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/tmp/interface
e-1.0-SNAPSHOT.jar!/BOOT-INF/classes!/.keystoreat org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:215)at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:53)
出现以上问题的原因:
打包后Spring无法使用resource.getFile()访问JAR中的路径的文件,必须使用resource.getInputStream()。
修正代码:
通过resource.getInputStream()获取证书文件,然后存储到临时文件,最后Http11NioProtocol 实例通过这个临时文件的路径传入值,这样此Http11NioProtocol 实例可以获取此证书文件。
private Connector createSslConnector() {Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();ClassPathResource resource = new ClassPathResource(".keystore");// 临时目录String tempPath =System.getProperty("java.io.tmpdir") + System.currentTimeMillis()+".keystore";File f = new File(tempPath);logger.info(".keystore目录临时存储路径" + tempPath);try {// 将key的值转存到临时文件IOUtils.copy(resource.getInputStream(),new FileOutputStream(f));connector.setScheme("https");connector.setSecure(true);connector.setPort(port);protocol.setSSLEnabled(true);// 指向临时文件protocol.setKeystoreFile(f.getAbsolutePath());protocol.setKeystorePass(keystorePass);protocol.setKeyAlias(keyAlias);protocol.setKeyPass(keyPass);} catch (IOException e) {e.printStackTrace();}return connector;}
问题备忘: class path resource [xx] cannot be resolved to absolute file path because it does not reside相关推荐
- springboot打成jar后获取resources下文件失败, cannot be resolved to absolute file path because it does not resid
读取resources下的文件quotaShow.jasper 本地开发环境能正常下载: ClassPathResource resource = new ClassPathResource(&quo ...
- 导出报错cannot be resolved to absolute file path because it does not reside in the file system
SpringBoot项目打包部署,读取jar里面的文件报错500,异常日志关键提示 cannot be resolved to absolute file path because it does n ...
- ueditor百度富文本编辑器linux下报错: class path resource [config.json] cannot be resolved to absolute file path
https://www.cnblogs.com/findtasy/p/10043273.html
- class path resource [spring/] cannot be resolved to URL because it does not exist问题解决(IDEA)
问题 Caused by: java.io.FileNotFoundException: class path resource [spring/] cannot be resolved to URL ...
- 文件系统中文件的最长(字符数)绝对路径 Longest Absolute File Path
为什么80%的码农都做不了架构师?>>> 问题: Suppose we abstract our file system by a string in the followin ...
- [Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- Leet Code OJ 388. Longest Absolute File Path [Difficulty: Medium]
题目 Suppose we abstract our file system by a string in the following manner: The string "dir\n\t ...
- class path resource [mapper/] cannot be resolved to URL because it does not exist解决办法
此错误信息为:无法将类路径资源[mapper/]解析为URL,因为它不存在. 由此可知在我们的applicationContext.xml文件中的mapper路径配置不正确 我是在我的classpat ...
- LeetCode Longest Absolute File Path(栈和前缀和解法)
问题:给出一个字符串表示的文件目录信息,使用\n\t...来表示层次关系.要求最长的绝对文件路径长度 思路: 方法一使用栈,当栈为空时,将文件信息直接放入栈中,如果当前文件的层次比栈顶的层次深则直接入 ...
最新文章
- SpringBoot中@ControlAdvice的使用
- 关于我的Android 博客
- linux内核7大功能,Linux Kernel5.10十个值得关注的功能
- 什么是网络推广浅析如何提高搜索引擎的抓取频次?
- 环境调试: RuntimeWarning: Couldn‘t find ffmpeg or avconv - defaulting to ffmpeg, but may not work warn(“
- iOS自动化打包之重签名导出不同证书ipa探索
- HTML文本应当存储为UTF-8无BOM格式!
- java ftp上传文件 linux_Java实现把文件上传至ftp服务器
- HTML输入学生成绩并排序java_JS实现冒泡排序,插入排序和快速排序并排序输出...
- 剑指offer之51-55题解
- 机器学习——对三种模式的看法
- 释放被束缚的页面 – V1.1.0
- Java程序员最厉害的是什么,「解密」谁是世界上最好的java程序员?
- 阅读笔记《全景探秘游戏设计艺术》
- Phonegap 之 iOS银联在线支付(js调用ios端银联支付控件)
- Cavium OCTEON网络处理器的安全性能
- tpx色卡电子版_pantone色卡电子版-pantone色卡中文版 3.0 免费版 - 河东下载站
- Python爬虫入门教程13:高质量电脑桌面壁纸爬取
- 个人最喜欢的几款火狐扩展
- 计算机毕业设计、计算机课程设计怎么做?计算机设计1900套来帮你!