spring boot ,spring cloud 下载resource 目录下的excel模板文件

感情迁移 提交于 2020-01-04 03:01:52
 /**
     * 下载模板
     * @throws Exception
     */
    @RequestMapping(value = "/downloadExcel")
    @ResponseBody
    public void downloadExcel(HttpServletResponse response, HttpServletRequest req) {

        try {
            ClassPathResource classPathResource = new ClassPathResource("template/impCfmd.xls");
            InputStream inputStream = classPathResource.getInputStream();
            response.setContentType("application/zip");
            OutputStream out = response.getOutputStream();
            response.setHeader("Content-Disposition", "attachment; filename=impCfmd.xls");
            int b = 0;
            byte[] buffer = new byte[1024*1024];
            while (b != -1) {
                b = inputStream.read(buffer);
                if(b!=-1) out.write(buffer, 0, b);
            }
            inputStream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            logger.error("downloadExcel error!",e);
        }
    }

代码如上:文件放在

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!