需求:提供接口下载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. 性能报告之HTML5 性能测试报告

    1. 引言 1.1. 编写目的 HTML5 作为当前"最火"的跨平台.跨终端(硬件)开发语言,越来越受到前端开发者 的重视,无论是 PC 端还是当前"火热"的移 ...

  2. 【NX二次开发】切换模块的方法,切换到制图模块

    源码(NX12.0): Session theSession = NXOpen::Session::GetSession(); theSession->ApplicationSwitchImme ...

  3. 【NX二次开发】指定矢量控件,记住上次选择的方向

    block UI控件如果有RetainValue属性,就用这个属性.没有这个属性可以参考下面这种方法.以矢量控件为例: 1.在apply_cb回调中,将控件值保存到文本中 double TopForT ...

  4. 题解 P6622 [省选联考 2020 A/B 卷] 信号传递

    洛谷 P6622 [省选联考 2020 A/B 卷] 信号传递 题解 某次模拟赛的T2,考场上懒得想正解 (其实是不会QAQ), 打了个暴力就骗了\(30pts\) 就火速溜了,参考了一下某位强者的题 ...

  5. Winform中只运行运行一个实例的方法

    在Program类的main方法按如下代码编写即可 1 static void Main() 2 { 3 if (Process.GetProcessesByName(Process.GetCurre ...

  6. 滑动窗口经典题 leetcode 3. 无重复字符的最长子串

    题目 解题思路 题目要求找出给定字符串中不含有重复字符的最长子串的长度.这是一个典型的滑动窗口的题目,可以通过滑动窗口去解答. 滑动窗口 具体操作如下图示:找到一个子串 s[left...right] ...

  7. 为Centos系统打补丁

    4.1.操作系统打补丁: 1.centos更新说明: centos 官方没有发布关于centos操作系统的补丁. centos 操作系统更新可以参考如下建议. 2.更新操作系统软件包,操作系统版本.内 ...

  8. Destroying The Graph 最小点权集--最小割--最大流

    Destroying The Graph 构图思路: 1.将所有顶点v拆成两个点, v1,v2 2.源点S与v1连边,容量为 W- 3.v2与汇点连边,容量为 W+ 4.对图中原边( a, b ), ...

  9. 暑假自学java第七天

    1,Object类: 任何类的父类都是Object 任何子类的对象都可以赋值给父类的引用.任何类的所有实例都可以用Object来代替 (3条消息) java中的Object类_iqqcode-CSDN ...

  10. Spring:Spring中bean的生命周期

    Spring中,从BeanFactory或ApplicationContext取得的实例为Singleton(单例模式),就是预设为每一个Bean的别名只能维持一个实例,而不是每次都产生一个新的对象使 ...