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 分钟前
Vue 3 vs Vue 2 深度解析:从架构革新到开发体验全面升级
前端·vue.js·架构
栀秋66614 分钟前
深入浅出链表操作:从Dummy节点到快慢指针的实战精要
前端·javascript·算法
Lueeee.15 分钟前
内核menuconfig配置实验
linux
遇见火星28 分钟前
常见Systemctl语句
linux·服务器·网络·systemctl
狗哥哥30 分钟前
Vue 3 动态菜单渲染优化实战:从白屏到“零延迟”体验
前端·vue.js
青青很轻_32 分钟前
Vue自定义拖拽指令架构解析:从零到一实现元素自由拖拽
前端·javascript·vue.js
树下水月38 分钟前
纯HTML 调用摄像头 获取拍照后的图片的base64
前端·javascript·html
蜗牛攻城狮42 分钟前
Vue 中 `scoped` 样式的实现原理详解
前端·javascript·vue.js
月明长歌42 分钟前
【码道初阶】【LeetCode 110】平衡二叉树:如何用一个“Magic Number”将复杂度从O(N²)降为 O(N)?
linux·算法·leetcode
专家大圣44 分钟前
摆脱局域网束缚!Neko+cpolar 让跨网共享成日常
服务器·网络·docker·内网穿透·cpolar