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 小时前
LangGraph的介绍
java·开发语言
qq_4798754312 小时前
netlink(1)
linux·服务器·网络
康小庄12 小时前
Java阻塞队列——用法及常用场景
java·开发语言·数据库·spring boot·spring·jetty
鳄鱼杆12 小时前
虚拟机 | 如何通过域名访问虚拟机中的Web服务?
前端
yy.y--12 小时前
Java多线程实例:输出线程名20次
java·开发语言
hzulwy13 小时前
Linux网络配置与测试
linux·运维·网络
We་ct13 小时前
LeetCode 236. 二叉树的最近公共祖先:两种解法详解(递归+迭代)
前端·数据结构·算法·leetcode·typescript
SakitamaX13 小时前
Tomcat介绍与实验
java·tomcat
zxnbmk13 小时前
磁盘挂载与迁移【自用复制】
linux