[英]Classpath resource not resolving in spring boot
我正在使用Spring Boot,并且资源文件夹中有一个文件。 我正在使用数字海洋机,并且当我使用java -jar mywebapp.war运行应用程序时,无法从类路径访问文件。 我正在使用以下标准语法进行访问:
File file = new ClassPathResource("mfile").getFile();
我收到错误消息,指出类路径资源无法解析为绝对路径。 我看到的问题是它正在显示路径! 标记如下:
/home/u/webapp/target/mywebapp.war!/WEB-INF/classess!/mfile
我在这里做错了什么?
由于使用java -jar
运行它,因此应将其构建为JAR文件而不是WAR。
了解更多: https : //docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html
作为jar运行时,获取文件不起作用。您应将其作为资源流获取。
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("/file.xsd") ;
File file = File.createTempFile("file", ".xsd");
try {
FileUtils.copyInputStreamToFile(inputStream, file);
} finally {
IOUtils.closeQuietly(inputStream);
它会为您提供文件。 如果要求是作为文件获取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.