webflux RouterFunction方式下载文件demo

原创
2020/11/20 09:58
阅读数 1.2K
public Mono<ServerResponse> test(ServerRequest request) throws Exception {
   
        File excel = new File("tmp");
        var out = new FileOutputStream(excel);
        var writer = new ExcelWriter(out, ExcelTypeEnum.XLSX,false);
        Sheet sheet1 = new Sheet(1, 0);
        sheet1.setSheetName("第一个sheet");
        writer.write(Arrays.asList(), sheet1);
        writer.finish();
        return ServerResponse.ok()
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=test.xlsx")
            .contentType(new MediaType("multipart/form-data"))
            .body((p, a) -> {
   
                var resp = (ZeroCopyHttpOutputMessage) p;
                return resp.writeWith(excel, 0, excel.length());
            }).doFinally(a -> {
   excel.deleteOnExit();});
    }
展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部