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());
    }
相关推荐
Hashan6 分钟前
Elpis:抽离业务代码,发布NPM包
前端·javascript·vue.js
quikai198111 分钟前
python练习第六组
java·前端·python
222you12 分钟前
线程的常用方法
java·开发语言
用户479492835691514 分钟前
0.1加0.2为什么不等于0.3-答不上来的都挂了
前端·javascript·面试
rit843249915 分钟前
C#实现的远程控制系统
前端·javascript·c#
Vect__21 分钟前
初识操作系统
linux
若风的雨22 分钟前
pcie bar 地址对齐规则
linux
诺斯贝克24 分钟前
Unable to create converter for xxx.NetworkResponse<Auth> for method AuthService
前端·后端
listhi52024 分钟前
针对燃油运输和车辆调度问题的蚁群算法MATLAB实现
前端·算法·matlab
LRX_19892725 分钟前
华为设备配置练习(七)VRRP 配置
服务器·网络·华为