SpringBoot读取Resource下文件的几种方式(十五)
需求:提供接口下载resources目录下的模板文件,(或者读取resources下的文件)给后续批量导入数据提供模板文件。
方式一:ClassPathResource
//获取模板文件:注意此处需要配置pom.xml文件;因为spring-boot默认只会读取application.yml配置文件
ClassPathResource classPathResource = new ClassPathResource(examplePath);
File file = null;
try {
file= classPathResource.getFile();
} catch (IOException e) {
e.printStackTrace();
}
模板文件位置

坑1:找不到模板文件staffTemplate.xlsx。
原因:maven默认只编译默认配置文件格式的文件,如yml。
解决:pom.xml 增加下面配置
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.xlsx</include>
</includes>
</resource>
</resources>
</build>
坑2:中文文件名下载后无法正常显示。
解决:将中文编码
将
response.setHeader("Content-Disposition", "attachment;fileName=批量上传用户模板.xlsx");
//String fileName=new String("批量上传用户模板".getBytes(), StandardCharsets.ISO_8859_1);
改为
response.setHeader("Content-Disposition", "attachment;fileName=" + new String("批量上传用户模板".getBytes(), StandardCharsets.ISO_8859_1)
+ ".xlsx");
参考链接:https://blog.csdn.net/weixin_42410936/article/details/106126377
问题:我通过这种方式,在本地可以找到路径,升到测试环境就不可以了。
SpringBoot读取Resource下文件的几种方式(十五)的更多相关文章
- SpringBoot读取Resource下文件的几种方式
https://www.jianshu.com/p/7d7e5e4e8ae3 最近在项目中涉及到Excle的导入功能,通常是我们定义完模板供用户下载,用户按照模板填写完后上传:这里模板位置resour ...
- 微信退款SpringBoot读取resource下的证书
微信支付退款接口,需要证书双向验证,测试的时候证书暂时放在resource下,上图 起初MyConfig中我是这样,在本机IDE中运行没有问题 但到Linux服务器的docker中运行就IO异常了,查 ...
- springboot读取resource下的文件
public static String DEFAULT_CFGFILE = ConfigManager.class.getClassLoader().getResource("conf/s ...
- springboot 读取 resource 下的文件
ClassPathResource classPathResource = new ClassPathResource("template/demo/200000168-check-resp ...
- Spring Boot 读取 resource 下文件
支持linux下读取 import org.springframework.core.io.ClassPathResource; public byte[] getCertStream(String ...
- 读取resource下文件
ArrayList<PatrolOper> patrolOpers = new ArrayList<>(); String jsonData = null; File json ...
- springboot读取resource下的文本文件
https://blog.csdn.net/programmeryu/article/details/58002218 文本所在位置: 获取ZH.txt: File file = ResourceUt ...
- python 读取wav 音频文件的两种方式
python 中,常用的有两种可以读取wav音频格式的方法,如下所示: import scipy from scipy.io import wavfile import soundfile as sf ...
- 读取本地json文件另一种方式
function getScenemapData(){ $.ajax({ url: "/js/currency.json", type: "GET" ...
随机推荐
- Java-数组拷贝
数组拷贝 首先了解深拷贝 浅拷贝数组的四种拷贝方式: 1.for循环拷贝 代码示例: import java.util.Arrays; public class TestDemo{ public st ...
- MongoDB学习笔记01:入门
MongoDB简介 MongoDB是一个开源.高性能.无模式的文档型数据库,当初的设计就是用于简化开发和方便扩展,是NoSQL数据库产品中的一种.是最 像关系型数据库(MySQL)的非关系型数据库. ...
- 「题解」黑暗塔 wizard
本文将同步发布于: 洛谷博客: csdn: 博客园: 简书. 题目 题意简述 给定 \(y\),求 \(\varphi(x)=y\) 中 \(x\) 的个数和最大值. \(1\leq y\leq 10 ...
- SpringBoot面试题 (史上最全、持续更新、吐血推荐)
文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...
- Vue——v-for动态绑定id的问题
问题:在Vue中,会遇到许多个多选框,倘若数量很庞大那么一个一个input框.label节点寻找,这样操作很繁琐. 直接上解决方案吧: html页面: <ul v-for="(item ...
- CDQ分治(三维偏序集)
排序,三关键字 去重 归并排序+树状数组 #include<bits/stdc++.h> using namespace std; #define re register int cons ...
- 整合Spring Cloud Stream Binder与GCP Pubsub进行消息发送与接收
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 之前的文章<整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收& ...
- Centos7安装配置jenkins(Tomcat)
Centos7安装配置jenkins(Tomcat) 一.准备工作 1.1 安装JDK1.8 具体安装过程不在赘述. 1.2 下载jenkins的war包 jenkins官网下载地址:https:// ...
- 9.4、安装zabbix(1)
1.什么是zabbix: zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案: zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以 ...
- 使用VS调试时出现 :provider: Named Pipes Provider, error: 40 - 无法打开到 SQL Server 的连接 解决方案
首先检查链接的数据库名称是否正确 其二是看看你的主机名称由没有写对,有些写成 127.0.0.1会出错.我就是将sessionState中的127.0.0.1出错,改为自己的主机名称就OK啦