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());
    }
相关推荐
深紫色的三北六号3 小时前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
雨中飘荡的记忆5 小时前
ElasticJob分布式调度从入门到实战
java·后端
掘金安东尼6 小时前
让 JavaScript 更容易「善后」的新能力
前端·javascript·面试
掘金安东尼6 小时前
用 HTMX 为 React Data Grid 加速实时更新
前端·javascript·面试
SudosuBash7 小时前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
灵感__idea8 小时前
Hello 算法:众里寻她千“百度”
前端·javascript·算法
yinuo8 小时前
轻松接入大语言模型API -04
前端
袋鼠云数栈UED团队9 小时前
基于 Lexical 实现变量输入编辑器
前端·javascript·架构
cipher9 小时前
ERC-4626 通胀攻击:DeFi 金库的"捐款陷阱"
前端·后端·安全
UrbanJazzerati9 小时前
非常友好的Vue 3 生命周期详解
前端·面试