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 分钟前
【开题答辩全过程】以 高校竞赛试题库管理平台为例,包含答辩的问题和答案
java
南山乐只2 分钟前
Java并发原生工具:原子类 (Atomic Classes)
java·开发语言·后端
一颗青果3 分钟前
C++下的atomic | atmoic_flag | 内存顺序
java·开发语言·c++
宴之敖者、4 分钟前
Linux——指令(下)
linux
michael_ouyang5 分钟前
WebSocket 鉴权方案选型与 Electron 应用的最佳实践
前端·websocket·网络协议·electron
木叶子---8 分钟前
pdf生成排查记录与解决方案
java·pdf
抠脚学代码9 分钟前
Qt与Linux
linux·数据库·qt
翼龙云_cloud10 分钟前
阿里云渠道商:云服务计费 按量 包年 预留实例怎么选?
服务器·阿里云·云计算
Sylvia-girl11 分钟前
Java之异常
java·开发语言
xixixin_12 分钟前
【vue】中字符串与数组转换:为何首选 Computed 而非 Methods?
前端·javascript·vue.js