[英]How to create downloadable file api with vertx in memory files
我從 S3 下載文件並通過將其轉換為字節數組將它們保存在內存中。 我不明白如何使用這個字節數組並使用 vertx 創建下載 api。
這是我卡住的地方:
router.get("/api/download/:requestId").handler(this::downloadFile);
private void downloadSimulatorFile(RoutingContext routingContext) {
String requestId = routingContext.request().getParam("requestId");
JsonObject response = new JsonObject();
try {
byte [] array=downloadFileFromS3ByRequestId(requestId);
} catch (Exception e) {
log.error(e);
} finally {
routingContext.response()
.putHeader("content-type", "application/json")
.sendFile(..)
// here is where i stuck as sendFile only expects
Strings which symol path to a file
}
}
如何在不保存文件並將其保存在內存中的情況下修改它?
謝謝
我試過了,它有效:
req.response()
.putHeader("Content-Type", "application/json")
.end(Buffer.buffer(bytes));
用於將 xlsx 文件作為 vertx 中的字節數組發送
byte [] array=downloadFileFromS3ByRequestId(requestId);
response.putHeader(io.vertx.core.http.HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=file.xlsx");
response.putHeader(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_OCTET_STREAM);
response.end(Buffer.buffer(array));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.