下载放在resource下面的excel文件
2024-10-19 16:41:47
1、将excel文件放项目resources目录下
2、打包的时候排除指定后缀文件,否则打包时会出现文件损坏的情况
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
3、resource配置
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.xlsx</include>
</includes>
</resource>
</resources>
4、读取resources下的xlsx文件
try {
//获取模板文件
File sourceFile = ResourceUtils.getFile("classpath:bankNameTemplate.xlsx");
//转换为文件流
BufferedInputStream fs = new BufferedInputStream(new FileInputStream(sourceFile));
//指定默认下载名称
String fileName = "XXX.xlsx";
//配置response的头
response.reset();
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
try {
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//循环从文件中读出数据后写出,完成下载
byte[] b = new byte[1024];
int len;
while ((len = fs.read(b)) != -1) {
response.getOutputStream().write(b, 0, len);
}
fs.close();
} catch (Exception e) {
logger.error("下载Excel模板异常", e);
}
5.前端请求
window.location.href = "/exportExcelTemplate";
最新文章
- ShineTime - 带有 CSS3 闪亮特效的缩略图相册
- JS 数字转换为大写金额
- 基于redis的分布式锁
- HTML5移动开发中的input输入框类型
- SPL學習之SplDoublyLinkedList
- C语言_函数【转】
- Working with Python subprocess - Shells, Processes, Streams, Pipes, Redirects
- UOJ#440. 【NOIP2018】填数游戏 动态规划
- 搭建docker私有仓库
- inheritCombinedParasitic.js
- BZOJ2743 HEOI2012采花(离线+树状数组)
- Logistic Regression – Geometric Intuition
- 【转】C# Graphics类详解
- 搭建自己的 Docker 私有仓库服务
- 数组实例 find和filter差异
- 证件照制作:使用PS打印一寸照片
- Rhel5.5配置Centos yum源
- VS调试_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->;nBlockUse));崩溃原因及解决方法
- libevent源码深度剖析十
- 活动的生命周期 Android
热门文章
- libusb开发者指南
- jQuery file upload里面的_create的调用和_initEventHandlers的调用
- webpack安装大于4.x版本(没有配置webpack.config.js)
- leetcode-mid-backtracking-17. Letter Combinations of a Phone Number
- mysql命令使用2
- leetcode 27. 移除元素(python)
- python调用c/c++时传递结构体参数
- .NET中使用EF6与连接MYSQL
- 中国MOOC_零基础学Java语言_第5周 数组_1多项式加法
- java 发送 http post 和 get 请求(利用unirest)