java.io.FileNotFoundException:无法解析类路径资源[certs/dev/test.crt]
我的springboot war应用程序通过Elastic beanstalk部署到ec2实例。我试图从resources文件夹中读取一个文件,并出现以下异常。证书文件夹位于资源文件夹内 Apr 8 22:42:03 web:java.io.FileNotFoundException:类路径资源[certs/dev/test.crt]无法解析为绝对文件路径,因为它不位于文件系统:jar:file:/var/app/current/target/test-app-0.0.1-SNAPSHOT.war/WEB-INF/classes/certs/dev/test.crtjava.io.FileNotFoundException:无法解析类路径资源[certs/dev/test.crt],java,spring,amazon-web-services,tomcat,Java,Spring,Amazon Web Services,Tomcat,我的springboot war应用程序通过Elastic beanstalk部署到ec2实例。我试图从resources文件夹中读取一个文件,并出现以下异常。证书文件夹位于资源文件夹内 Apr 8 22:42:03 web:java.io.FileNotFoundException:类路径资源[certs/dev/test.crt]无法解析为绝对文件路径,因为它不位于文件系统:jar:file:/var/app/current/target/test-app-0.0.1-SNAPSHOT.wa
import com.amazonaws.services.iot.client.sample.sampleUtil.SampleUtil;
private ResourceLoader resourceLoader;
Resource certRes = resourceLoader.getResource("classpath:certs/dev/test.crt");
Resource privateRes = resourceLoader.getResource("classpath:certs/dev/private.key");
String certificateFile = certRes.getFile().getPath();
String privateKeyFile = privateRes.getFile().getPath();
SampleUtil.KeyStorePasswordPair pair = SampleUtil.getKeyStorePasswordPair(certificateFile, privateKeyFile);
我尝试使用ClassLoader.getSystemClassLoader().getResourceAsStream读取文件,但SampleUtil.KeyStorePasswordPair需要一个路径
有人能帮忙吗
在错误中,我看到了“jar:file”,但我的应用程序是war。这是个问题吗
Ann不能将类路径资源转换为
java.util.File
。后者需要是文件系统上的实际文件资源。当WAR/JAR中有东西时,情况并非如此,因此getFile
将不起作用。您只能使用InputStream
来读取它,或者需要找到不同的方法来转换为路径
,或者使用不同的方法来生成SampleUtil.keystrepasswordpair
。此外-您是否检查了该文件是否实际位于war文件中?请参阅此链接。有一个选项可以将类路径资源作为jar文件中的输入流读取。