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" ...
随机推荐
- 【UG二次开发】获取对象类型 UF_OBJ_ask_type_and_subtype
代码: int type=0, subtype=0; UF_OBJ_ask_type_and_subtype(objTag, &type, &subtype);
- moment常用方法
1.subtract方法,时间加减处理 console.log(moment().format("YYYY-MM-DD HH:mm:ss")); //当前时间 console.lo ...
- Xmanager6 企业版安装
Xmanager6 企业版安装 链接:https://pan.baidu.com/s/1QZOD0iPd4WbVHBVXIbJ-fw 提取码:ebkl 一.安装教程 1.1 下载解压,双击安装exe主 ...
- 手把手教你IDEA连接码云(Gitee)
目录 前言 一.下载.安装git 1.打开git官网,选择你的操作系统 2.根据你的系统位数选择相应的版本下载 3.安装 4.配置全局的用户名.邮箱 5.在idea中配置git目录 二.配置Gitee ...
- Unix、Linux 软件包管理快速入门对照:apt、brew、pkg、yum
请访问原文链接:https://sysin.org/blog/apt-brew-pkg-yum/,查看最新版.原创作品,转载请保留出处. 作者:gc(at)sysin.org,主页:www.sysin ...
- 企业实施CRM系统 创造更多利润 - Zoho CRM
对企业来说,客户关系是一种投资.我们都知道企业的资源是有限的,因此必须要将这些有限的资源投入到能够带来持续价值的客户身上.而只有良好的客户关系才能够提高客户的忠诚度,多次购买甚至溢价购买企业的产品,持 ...
- AOP面向切面的实现
AOP(Aspect Orient Programming),我们一般称为面向方面(切面)编程,作为面向对象的一种补充,用于处理系统中分布于各个模块的横切关注点,比如事务管理.日志.缓存等等. AOP ...
- [心得]docker学习笔记
1. docker是什么??? (1) docker是一台类似虚拟机的功能, 内部由一个个镜像组成, 镜像里可以运行容器, 而这个容器可以是任何东西, 比如mysql, 比如tomcat等等, 它的目 ...
- SpringMVC(6)数据验证
在系列SpringMVC(4)数据绑定-1.SpringMVC(5)数据绑定-2中我们展示了如何绑定数据,绑定完数据之后如何确保我们得到的数据的正确性?这就是我们本篇要说的内容 -> 数据验证. ...
- Linux date 获取时间
获取当前日期: ubuser@ubuser-OptiPlex-7010:~$ date +%Y_%m_%d2020_12_16 获取当前时间: ubuser@ubuser-OptiPlex-7010: ...