SpringCloud之实现下载Excel模板文件

页面代码:

function downloadTemplate() {
        window.location.href=path+"/web/api/excel/open/downloadTemplate?accessToken=${accessToken}"
    }

后台代码如下:

/**
     * 下载excel模板
     *
     * @param response
     */
    @ResponseBody
    @RequestMapping("/open/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response, HttpSession session) {
        try {
            InputStream inputStream = (InputStream) this.getClass().getClassLoader().getResourceAsStream("base.xlsx");
            response.setContentType("application/zip");
            OutputStream out = response.getOutputStream();
            response.setHeader("Content-Disposition", "attachment; filename="+"base"+".xlsx");
            int b = 0;
            byte[] buffer = new byte[1000000];
            while (b != -1) {
                b = inputStream.read(buffer);
                if(b!=-1) out.write(buffer, 0, b);
            }
            inputStream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("模板下载失败");
        }
    }

其中,base.xlsx文档就在项目的Resources目录下

你可能感兴趣的:(SpringCloud,Java操作excel)