下载模板

    @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();
                }
            }
        }

    }

 

上一篇:ftp 报错收集


下一篇:后台技术学习4--会话技术