需求:提供接口下载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下文件的几种方式(十五)的更多相关文章

  1. SpringBoot读取Resource下文件的几种方式

    https://www.jianshu.com/p/7d7e5e4e8ae3 最近在项目中涉及到Excle的导入功能,通常是我们定义完模板供用户下载,用户按照模板填写完后上传:这里模板位置resour ...

  2. 微信退款SpringBoot读取resource下的证书

    微信支付退款接口,需要证书双向验证,测试的时候证书暂时放在resource下,上图 起初MyConfig中我是这样,在本机IDE中运行没有问题 但到Linux服务器的docker中运行就IO异常了,查 ...

  3. springboot读取resource下的文件

    public static String DEFAULT_CFGFILE = ConfigManager.class.getClassLoader().getResource("conf/s ...

  4. springboot 读取 resource 下的文件

    ClassPathResource classPathResource = new ClassPathResource("template/demo/200000168-check-resp ...

  5. Spring Boot 读取 resource 下文件

    支持linux下读取 import org.springframework.core.io.ClassPathResource; public byte[] getCertStream(String ...

  6. 读取resource下文件

    ArrayList<PatrolOper> patrolOpers = new ArrayList<>(); String jsonData = null; File json ...

  7. springboot读取resource下的文本文件

    https://blog.csdn.net/programmeryu/article/details/58002218 文本所在位置: 获取ZH.txt: File file = ResourceUt ...

  8. python 读取wav 音频文件的两种方式

    python 中,常用的有两种可以读取wav音频格式的方法,如下所示: import scipy from scipy.io import wavfile import soundfile as sf ...

  9. 读取本地json文件另一种方式

    function getScenemapData(){ $.ajax({     url: "/js/currency.json",    type: "GET" ...

随机推荐

  1. 菜鸟刷题路(随缘刷题):leetcode88

    lc88 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int i = m - 1, j = ...

  2. Redis不是只有get set那么简单

    我以前还没接触Redis的时候,听到大数据组的小伙伴在讨论Redis,觉得这东西好高端,要是哪天我们组也可以使用下Redis就好了,好长一段时间后,我们项目中终于引入了Redis这个技术,我用了几下, ...

  3. MySQL 架构|给你一个“上帝视角”

    "我平时的工作就是 CRUD (增删改查)呀!我怎么提升自己的技术?"."平时开发我都是用开源的 MyBatis.Hibernate,连原生的 sql 我都没写过几行&q ...

  4. 深度学习的优化器(各类 optimizer 的原理、优缺点及数学推导)

    深度学习优化器 深度学习中的优化器均采用了梯度下降的方式进行优化,所谓炼丹我觉得优化器可以当作灶,它控制着火量的大小.形式与时间等. 初级的优化器 首先我们来一下看最初级的灶台(100 - 1000 ...

  5. ES6学习笔记之函数(二)

    5.作用域 使用默认参数时,参数会形成一个独立的作用域,此作用域与函数体中的作用域是平行关系,互不影响. var x = 1; function show(x, y= function () { x= ...

  6. python返回列表最大值(java返回数组最大值)

    b=["3","2","1","6","5","2","1" ...

  7. Pygame 入门基本指南

    最近正在利用 Python 制作一个小游戏,但对于 Pygame 不熟悉,故在学习的过程记录相关知识点 Pygame 中文文档下载:Here Pygame第1-1课:入门 什么是Pygame? Pyg ...

  8. Java核心基础第1篇-走进Java世界

    一.Java简介 1.1 Java概述 Java从一开始就以友好的语法.面向对象.内存管理和最棒的跨平台可移植性来吸引程序员. 写一次就可以在所有地方执行( write-once/run-anywhe ...

  9. flink数据广播场景总结

    数据集广播,主要分为广播变量,广播维表(数据集)两种,一种为变量,一种为常量(抽象的说法): 一.数据广播背景 对于小变量,小数据集,需要和大数据集,大流进行联合计算的时候,往往把小数据集广播出去,整 ...

  10. APDU:APDU常用指令

    APDU= ApplicationProtocol data unit, 是智能卡与智能卡读卡器之间传送的信息单元, (给智能卡发送的命令)指令(ISO 7816-4规范有定义) CLA INS P1 ...