下载的模板, 没有之一

    @PostMapping(value = "/template")
    @ApiOperation("xxxxxx")
    public void xxxxxxxxDownload(
            HttpServletResponse response
    ) throws Exception {
        try {
            Resource res = new ClassPathResource(xxxxxxxxx);
            try (BufferedInputStream fis = new BufferedInputStream(res.getInputStream())) {

                int offset = 0;
                int bytesRead = 0;
                byte[] data = new byte[fis.available()];
                while ((bytesRead = fis.read(data, offset, data.length - offset))
                        != -1) {
                    offset += bytesRead;
                    if (offset >= data.length) {
                        break;
                    }
                }
                //String str = new String(data, 0, offset, "UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + res.getFilename());
                response.setHeader("Access-Control-Allow-Origin", "*");
                //response.setContentType("application/vnd.ms-read; charset=utf-8");
                response.setContentType("application/octet-stream");
                try (OutputStream outStream = new BufferedOutputStream(response.getOutputStream())) {
                    outStream.write(data);
                    outStream.flush();
                }
            }
        } catch (Exception e) {
            logger.error("xxxxxx" + e.getMessage(), e);
            throw e;
        }
    }

 

posted @ 2019-04-15 16:44  multitude  阅读(203)  评论(0编辑  收藏  举报