File privateKeyFile = ResourceUtils.getFile(“classpath:wx.pfx”);
PrivateKey privateKey = getPrivateKey(privateKeyFile , privateKeyPassword);
未打包时都可以获取到根路径和文件,打包后报java.lang.NullPointerException
ClassPathResource resource = new ClassPathResource(“wx.pfx”);
InputStream inputStream = resource.getInputStream();
这是因为打包后Spring试图访问文件系统路径,但无法访问JAR中的路径。
因此必须使用resource.getInputStream()
本文探讨了在Spring框架下,未打包和已打包环境下资源文件加载的区别及解决方案。详细介绍了如何使用ClassPathResource和getInputStream()方法正确加载位于classpath下的文件,如证书文件wx.pfx,避免NullPointerException异常。

1万+

被折叠的 条评论
为什么被折叠?



