导出

作者: 一起DP吧 | 来源:发表于2019-07-31 09:37 被阅读0次

    通过生成File,达到下载文件的目的

    public R exportCsv(@RequestBody String[] ids, HttpServletResponse response) {
        logger.info("导出");
        try {
            File file = xxxService.exportCsv(ids);
            // 设置Content-Disposition
            response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());
            InputStream in = new FileInputStream(file.getPath());
            OutputStream out = response.getOutputStream();
            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            out.close();
            in.close();
            // 删除生成的文件
            xxxService.deleteFile(file);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("导出文件失败", e);
        }
        return null;
    }
    

    相关文章

      网友评论

          本文标题:导出

          本文链接:https://www.haomeiwen.com/subject/qeaarctx.html