如何在内存文件中创建带有vertx的可下载文件api

时间:2017-07-27 19:27:21

标签: java vert.x

我从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

            }
    }

如何在不保存文件的情况下修改此文件并将其保存在内存中?

由于

2 个答案:

答案 0 :(得分:1)

我试过了,它有效:

req.response()
  .putHeader("Content-Type", "application/json")
  .end(Buffer.buffer(bytes));

答案 1 :(得分:0)

用于将 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));