Vertx将CSV文件/数据流式传输到http响应

时间:2020-08-08 21:31:31

标签: java csv rx-java2 vert.x vertx-httpclient

我想从vertx API响应中以CSV文件的形式从数据库返回一些数据。我一直在关注this链接

但是我无法返回正确的CSV文件作为API响应。

我的代码:

rc.response()
        .putHeader("Content-Type", "application/csv")
        .putHeader("Content-Disposition", "attachment; filename=stream_wide.csv")
        .putHeader(HttpHeaders.TRANSFER_ENCODING, "chunked")
        .setChunked(true);

getDataFromDB()
        .subscribe(
            rs -> rc.response().write(rs, "UTF-8"),
            ex -> {ex.printStackTrace(); logger.error("exception  encountered " + ex.getMessage());},
            () -> {rc.response().end(); rc.response().close();});

1 个答案:

答案 0 :(得分:0)

您遇到什么样的错误/行为?

尝试使用CSV文件中的字节创建缓冲区。

byte [] csvBytes = <some method extracting the bytes representing the csv file from the result set>

response.end(Buffer.buffer(csvBytes));