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());
    }
相关推荐
Sylvia-girl1 小时前
IO流~~
java·开发语言
weixin199701080161 小时前
【性能提升300%】仿1688首页的Webpack优化全记录
前端·webpack·node.js
HypoxiaDream1 小时前
LINUX-Ext系列⽂件系统
linux·运维·服务器
小毛驴8501 小时前
Linux curl 命令用法
linux·运维·chrome
冰暮流星1 小时前
javascript之数组
java·前端·javascript
李斯啦果1 小时前
【Linux】Linux目录配置
linux·运维·服务器
Re.不晚1 小时前
JAVA进阶之路——无奖问答挑战3
java·开发语言
AI+程序员在路上1 小时前
linux下线程中pthread_detach与pthread_join区别
linux·运维·服务器
代码游侠1 小时前
C语言核心概念复习——C语言基础阶段
linux·开发语言·c++·学习
logocode_li1 小时前
说透 Linux Shell:命令与语法的底层执行逻辑
linux·运维·ssh