springboot读取jar包资源文件文件
(2024-01-12 08:48:28)
先修改pom.xml文件.使打包后的jar包含你的资源文件
在pom.xml
文件下的build选项中的src/main/resources
的目录下
添加配置
<<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">build>
<<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">plugins> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">plugin> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">groupId>org.springframework.boot</<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">groupId> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">artifactId>spring-boot-maven-plugin</<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">artifactId> </<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">plugin> </<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">plugins> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">resources> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">resource> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">directory>src/main/java</<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">directory> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">includes> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">include>***.xml</<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">include> </<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">includes> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">filtering>false</<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">filtering> </<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">resource> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">resource> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">directory>src/main/resources</<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">directory> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">includes> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">include>***.xml</<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">include> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">include>***.js</<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">include> <<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">include>**/*.html</<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">include>template/*.xlsx
</<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">includes>
<<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">filtering>
false</<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">filtering> </<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">resource> </<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">resources> </<span class="hljs-name" style="margin: 0px; padding: 0px; color: rgb(99, 163, 92);">build>
2:读取资源文件到硬盘,然后使用
@javax.annotation.Resource
private ResourceLoader resourceLoader;
String rootPath = "/home";
String configFilePath = rootPath + "/template/template.xlsx";
File configFile = new File(configFilePath);
if (!configFile.exists()) {
log.info("模板文件({})不存在,开始从resources里复制." + configFilePath);
copyResourceFile("classpath:/template/template.xlsx", configFilePath);
}
public void copyResourceFile(String source, String target) throws IOException {
Resource resource = resourceLoader.getResource("classpath:" + source);
InputStream input = resource.getInputStream();
File targetFile = new File(target);
FileUtils.copyInputStreamToFile(input, targetFile);
}