其他分享
首页 > 其他分享> > 下载模板

下载模板

作者:互联网

    @ApiOperation("导入模板下载")
    @GetMapping("/template/downLoad")
    public void exportExcelTemplate(HttpServletResponse response) {
        var fileName = "static/template.xls";
        var excelName = "template";
        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            String str = new String(excelName.getBytes("utf-8"),"iso-8859-1") + ".xls";
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-disposition", "attachment;fileName=" + str);

            inputStream = XXController.class.getClassLoader().getResourceAsStream(fileName);
            if(null == inputStream) throw new BizException("未找到模板");

            outputStream = response.getOutputStream();
            byte[] bytes = new byte[1024];
            int length = inputStream.read(bytes);
            while (length > 0) {
                outputStream.write(bytes, 0, length);
                length = inputStream.read(bytes);
            }
        } catch (Exception e) {
            log.error("{}",e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

 

标签:outputStream,bytes,inputStream,length,模板,null,response,下载
来源: https://www.cnblogs.com/smileblogs/p/15827077.html