简体   繁体   中英

Vertx Streaming csv file/data to http response

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();});

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM