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());
    }
相关推荐
橘子编程2 分钟前
React 19 全栈开发实战指南
前端·react.js·前端框架
DanCheOo3 分钟前
AI Streaming 架构:从浏览器到服务端的全链路流式设计
前端·agent
SPC的存折5 分钟前
1、MySQL故障排查与运维案例
linux·运维·服务器·数据库·mysql
Lyyaoo.5 分钟前
【JAVA基础面经】线程的状态
java·开发语言
Hello小赵6 分钟前
C语言如何自定义链接库——编译与调用
android·java·c语言
Run_Teenage6 分钟前
Linux:认识信号,理解信号的产生和处理
linux·运维·算法
我是小趴菜9 分钟前
前端如何让图片、视频、pdf等文件在浏览器直接下载而非预览
前端
希望永不加班10 分钟前
SpringBoot 配置绑定:@ConfigurationProperties
java·spring boot·后端·spring
悟空码字11 分钟前
MySQL性能优化的天花板:10条你必须掌握的顶级SQL分析技巧
java·后端·mysql
cg3314 分钟前
开源项目自动化:用 GitHub Actions 让每个 Issue 都被温柔以待
前端