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());
    }
相关推荐
meichaoWen1 分钟前
【nodejs】nodejs的一些基础知识
开发语言·前端·javascript
@Autowire3 分钟前
Grid-grid-template-areas 属性
前端·javascript·css
kaico20183 分钟前
ConcurrentHashMap源码分析
java
虫小宝5 分钟前
企业微信API接口对接中Java后端的模拟测试(Mock)与单元测试实战技巧
java·单元测试·企业微信
Tim_Van7 分钟前
彻底解决:80 端口 GET/POST 正常,PUT 却报 ERR_CONNECTION_RESET?
java·vue.js·spring boot·ruoyi·若依
amazing-yuan8 分钟前
彻底解决该 TS 报错 + 提升编译效率
前端·javascript·vue.js·typescript·vue·异常报错处理
乔冠宇9 分钟前
前端工程化——ESLint + Prettier 规范代码开发
前端
前端小白在前进9 分钟前
优雅的使用Nexent创建与部署前端面试智能体
前端·面试·职场和发展
元媛媛10 分钟前
UiPath |5个基础自动化场景
android·java·自动化
独自破碎E12 分钟前
Spring AI怎么实现结构化输出?
java·人工智能·spring