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());
    }
相关推荐
moshuying4 小时前
别让AI焦虑,偷走你本该有的底气
前端·人工智能
GIS之路5 小时前
ArcPy,一个基于 Python 的 GIS 开发库简介
前端
可夫小子7 小时前
OpenClaw基础-为什么会有两个端口
前端
喝拿铁写前端7 小时前
Dify 构建 FE 工作流:前端团队可复用 AI 工作流实战
前端·人工智能
喝咖啡的女孩8 小时前
React 合成事件系统
前端
从文处安8 小时前
「九九八十一难」组合式函数到底有什么用?
前端·vue.js
用户5962585736068 小时前
戴上AI眼镜逛花市——感受不一样的体验
前端
yuki_uix8 小时前
Props、Context、EventBus、状态管理:组件通信方案选择指南
前端·javascript·react.js
老板我改不动了8 小时前
前端面试复习指南【代码演示多多版】之——HTML
前端
panshihao8 小时前
Mac 环境下通过 SSH 操作服务器,完成前端静态资源备份与更新(全程实操无坑)
前端