当前位置: 首页 > news >正文

下载excel模板

从resource文件目录下面读取所需要下载的文件模板 

@GetMapping("/downloadTemplate")
public void downloadTemplate(HttpServletResponse response) {

    ServletOutputStream servletOutputStream = null;
    InputStream inputStream = null;
    try {
        String filename = "文件名.xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        response.addHeader("charset", "utf-8");
        response.addHeader("Pragma", "no-cache");
        String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
        response.setHeader("Content-Disposition", "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);

        inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/文件名.xlsx");
        servletOutputStream = response.getOutputStream();
        IOUtils.copy(inputStream, servletOutputStream);
        response.flushBuffer();
    } catch (Exception e) {
        e.printStackTrace();
        log.error("下载模板失败"+e.toString());
    } finally {
        try {
            if (servletOutputStream != null) {
                servletOutputStream.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("下载模板失败"+e.toString());
        }
    }
}

相关文章:

  • 非零基础自学Golang 第17章 HTTP编程(上) 17.5 知识拓展
  • MATLAB算法实战应用案例精讲-【连接分析】PageRank
  • ping命令使用之每次只ping一次
  • 【数据库内核分析系列】:数据库索引的创建过程
  • 非零基础自学Golang 第17章 HTTP编程(上) 17.1 HTTP简介
  • 5G数据库组网方案
  • 全国职业院校技能大赛中职组网络安全竞赛试题 —命令注入(笔记文档)
  • Zookeeper 4 Zookeeper JavaAPI 操作 4.9 模拟12306 售票案例
  • RabbitMQ学习初整理
  • 20221223英语学习
  • 电脑桌面文件删除了怎么找回?
  • 测试资源管控
  • 大数据处理之ClickHouse概述及架构参考(未完)
  • 实验室搬迁改造需要注意哪些
  • p5.js 光速入门
  • 成功转行Python工程师,年薪30W+,经验总结都在这!
  • 网络安全方向好吗?
  • 华为云CDN,助力电商平台无惧流量洪峰
  • Euler identity
  • 技能梳理32@电源防反接电路+光耦隔离电路+串口磁耦隔离电路