Java 如何使用vertx内存文件创建可下载的文件api
Iam从S3下载文件,并通过将其转换为字节数组将其保存在内存中。 我不明白如何使用这个字节数组并使用vertx创建下载api 这就是我坚持的地方:Java 如何使用vertx内存文件创建可下载的文件api,java,vert.x,Java,Vert.x,Iam从S3下载文件,并通过将其转换为字节数组将其保存在内存中。 我不明白如何使用这个字节数组并使用vertx创建下载api 这就是我坚持的地方: router.get("/api/download/:requestId").handler(this::downloadFile); private void downloadSimulatorFile(RoutingContext routingContext) { String requestId = rout
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));
用于在vertx中将xlsx文件作为字节数组发送
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));
为什么不将下载文件转换成流并发送该流作为响应。你知道怎么做吗?