下载excel模板

前言:

复制代码
			Excel模板在你当前项目相对路径下,如下图所示
			![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/643ffdb76d6f4769a5793ccf65d41739.png)

代码如下:(为了直观,统一放在控制层)

java 复制代码
/**
     * 下载模板
     * @param response
     * @return
     */
    @SneakyThrows
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response){
        String fileName = "指标体系导入模板.xlsx";
        ServletOutputStream out = response.getOutputStream();

        try{
            //文件在项目中的存放路径
            String filePath = getClass().getResource("/static/" + fileName).getPath();    // 文件路径就是上图添的位置
            filePath = URLDecoder.decode(filePath, "UTF-8");

            //设置响应
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Accept-Ranges", "bytes");
            response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
            FileInputStream inputStream = new FileInputStream(filePath);

            int b = 0;
            byte[] buffer = new byte[1024];
            while ((b = inputStream.read(buffer)) != -1) {
                // 写到输出流(out)中
                out.write(buffer, 0, b);
            }
            inputStream.close();
        }catch (Throwable e){
            e.printStackTrace();
        }finally{
            try {
                if (out != null) {
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
相关推荐
lulu121654407814 分钟前
Claude Code项目大了响应慢怎么办?Subagents、Agent Teams、Git Worktree、工作流编排四种方案深度解析
java·人工智能·python·ai编程
Ares-Wang37 分钟前
Flask》》 Flask-Bcrypt 哈希加密
后端·python·flask
kongba0071 小时前
项目打包 Python Flask 项目发布与打包专家 提示词V1.0
开发语言·python·flask
froginwe111 小时前
C 语言测验
开发语言
belldeep2 小时前
介绍 遗传算法 与 TSP问题
python·遗传算法·ga·tsp问题
解救女汉子2 小时前
SQL触发器如何获取触发源应用名_利用APP_NAME函数追踪
jvm·数据库·python
今夕资源网2 小时前
powershell工具包 安装升级脚本并设置UTF-8 环境快捷方式创建 将powershell的编码默认改为UTF-8
开发语言·utf-8·powershell·utf-8编码·powershell7·powershell5·设置utf-8编码
机器视觉知识推荐、就业指导2 小时前
Qt:真正的门槛不是入门,而是维护
开发语言·qt
hhb_6182 小时前
Dylan 语言核心特性与工程实践深度解析
开发语言·c#
思绪无限2 小时前
YOLOv5至YOLOv12升级:血细胞检测系统的设计与实现(完整代码+界面+数据集项目)
人工智能·python·深度学习·目标检测·计算机视觉·yolov12·血细胞检测