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

huangapple go评论70阅读模式
英文:

Vertx Streaming csv file/data to http response

问题

以下是您要翻译的内容:

我想在Vertx API响应中将一些数据作为CSV文件返回给DB。我一直在遵循此链接,但我无法返回正确的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();});

链接

英文:

I want to return some data from DB as a CSV file in the vertx API response. I have been following this link

But I'm not able to return proper CSV file as API response.

My code:

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

得分: 1

你遇到了什么错误/行为?

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

byte [] csvBytes = <一些从结果集中提取表示CSV文件的字节的方法>

response.end(Buffer.buffer(csvBytes));
英文:

What kind of error/behavior are you getting?

Try creating a Buffer with the bytes from the CSV file.

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

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

huangapple
  • 本文由 发表于 2020年8月9日 05:31:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63320415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定