5分钟下载excel模板

复制代码
@PostMapping("/downloadTemplate")
@Operation(summary="下载入职excel模板")
public void downloadExcel(HttpServletResponse response){
    try{
        //获取输入流,原始模板位置
        String filePath = "template/gridPersonTemplate.xlsx";
        ClassPathResource resource = new ClassPathResource(filePath);
        InputStream bis = resource.getInputStream();
        //假如以中文名下载的话,设置下载文件名称
        String filename = "gridPersonTemplate1.xlsx";
        //转码,免得文件名中文乱码
        filename = URLEncoder.encode(filename,"UTF-8");
        //设置文件下载头
        response.addHeader("Content-Disposition", "attachment;filename=" + filename);
        //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
        response.setContentType("multipart/form-data");
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
        int len = 0;
        while((len = bis.read()) != -1){
            out.write(len);
            out.flush();
        }
        out.close();
    }catch (Exception e){
        log.error(e.getMessage());
    }
相关推荐
小李独爱秋7 分钟前
模拟面试:说一下数据库主从不同步的原因。
运维·服务器·mysql·面试·职场和发展·性能优化
白云偷星子17 分钟前
RHCSA笔记7
linux·笔记
tryCbest27 分钟前
Linux常用命令V2026
linux·运维
沙白猿1 小时前
【TJXT】Day3
java·开发语言
百锦再1 小时前
Java的TCP和UDP实现详解
java·spring boot·tcp/ip·struts·spring cloud·udp·kafka
毕设源码-朱学姐1 小时前
【开题答辩全过程】以 高校实验室管理系统为例,包含答辩的问题和答案
java
A_B_C_Q1 小时前
StringBuilder 与 StringBuffer的区别
java·前端
用户908324602731 小时前
Spring AI 1.1.2 集成 MCP(Model Context Protocol)实战:以 Tavily 搜索为例
java·后端
洋洋技术笔记1 小时前
vue3+vite+elementplus简单介绍
前端
Joker Zxc1 小时前
【前端基础(Javascript部分)】2、JavaScript的变量和数据类型
开发语言·前端·javascript