webflux RouterFunction方式下載文件demo

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();});
    }
相關文章
相關標籤/搜索