下载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();
            }
        }
    }
相关推荐
2501_944875512 分钟前
Go后端工程师
开发语言·后端·golang
该用户已不存在6 分钟前
没有这7款工具,难怪你的Python这么慢
后端·python
听风吟丶9 分钟前
Java 反射机制深度解析:从原理到实战应用与性能优化
java·开发语言·性能优化
serve the people11 分钟前
tensorflow 零基础吃透:RaggedTensor 的不规则形状与广播机制 2
人工智能·python·tensorflow
Hello.Reader12 分钟前
Flink ML 基本概念Table API、Stage、Pipeline 与 Graph
大数据·python·flink
chen_note14 分钟前
Python面向对象、并发编程、网络编程
开发语言·python·网络编程·面向对象·并发编程
她说彩礼65万15 分钟前
C# params使用
开发语言·c#·log4j
信看15 分钟前
树莓派CAN(FD) 测试&&RS232 RS485 CAN Board 测试
开发语言·python
brent42316 分钟前
DAY24推断聚类后簇的类型
python
测试199820 分钟前
一个只能通过压测发现Bug
自动化测试·软件测试·python·selenium·测试工具·bug·压力测试