下载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();
            }
        }
    }
相关推荐
一起逃去看海吧3 分钟前
dify-03
java·linux·开发语言
郑洁文26 分钟前
面向Web安全的Python渗透测试系统设计与实现
python·安全·web安全
情绪总是阴雨天~1 小时前
智能语音分析Agent项目
python·自动化·fastapi·langgraph
Xin_ye100861 小时前
C# 零基础到精通教程 - 第十八章:部署与发布——让应用上线
开发语言·c#
思麟呀2 小时前
C++11并发编程:call_once一次性执行+atomic原子类型+CAS无锁编程+自旋锁
linux·开发语言·jvm·c++·windows
Dxy12393102162 小时前
Django 数据库 ENGINE 完全指南:选错了,性能差 10 倍
python·django
码不停蹄的玄黓2 小时前
Java 生产者-消费者模型详解
java·开发语言·python
爱讲故事的2 小时前
操作系统第一讲复习:为什么学习操作系统,以及操作系统到底在做什么?
linux·开发语言·windows·学习·ubuntu·c#
笨蛋不要掉眼泪2 小时前
Java并发编程:Executors框架类深度解析
java·开发语言·并发
凯瑟琳.奥古斯特3 小时前
力扣1235:加权区间调度最优解
java·python·算法·leetcode·职场和发展