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());
    }
相关推荐
芝士雪豹只抽瑞克五12 小时前
Nginx 高性能Web服务器笔记
服务器·nginx
精神状态良好13 小时前
实战:从零构建本地 Code Review 插件
前端·llm
荒诞英雄13 小时前
Vue3 Teleport我真是没招了
前端·vue.js
YAY_tyy13 小时前
2025 最新版 Node.js 下载安装及环境配置教程
前端·node.js·教程·工具配置
百思可瑞教育13 小时前
Vue 前端与 Node.js 后端文件上传与处理实现
前端·javascript·vue.js·前端框架·node.js·ecmascript·百思可瑞教育
玹外之音13 小时前
Spring AI MCP 无状态服务器实战:构建AI智能用户管理系统
java·后端
java干货13 小时前
Redis 的 ZipList 是什么?它是怎么解决内存碎片问题的?
java
架构师汤师爷13 小时前
一文彻底搞懂 OpenClaw 的架构设计与运行原理(万字图文)
前端·agent
苑若轻航13 小时前
防抖和节流:解决高频事件性能
前端
失重外太空啦13 小时前
Tomcat
java·服务器·tomcat