加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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);
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有