gpt4 book ai didi

java - 如何从vertx中的文件中获取byte[]

转载 作者:行者123 更新时间:2023-11-29 04:18:24 25 4
gpt4 key购买 nike

我正在尝试从一个文件中提取 byte[],我将在 post 调用中以 form-data 的形式接收该文件。但是,我没有找到任何有用的代码片段来帮助我获取 byte[]。下面是我正在尝试的代码

public static void main( String[] args )
{
Vertx vertx =Vertx.vertx();
HttpServer httpServer= vertx.createHttpServer() ;

Router router=Router.router(vertx);
router.route().handler(BodyHandler.create().setUploadsDirectory("uploads"));

router.post("/form").handler(ctx -> {
ctx.response().putHeader("Content-Type", "text/plain");

ctx.response().setChunked(true);
MultiMap attributes = ctx.request().formAttributes();
Set<io.vertx.ext.web.FileUpload> uploads = ctx.fileUploads();
ctx.response().end();
}

httpServer
.requestHandler(router::accept)
.listen(8091);
}

此处文件正在上传到服务器的上传文件夹中。但是,我不想在服务器上上传文件,而是希望文件的 byte[] 传输到不同的服务进行上传。所以如果我评论这一行router.route().handler(BodyHandler.create().setUploadsDirectory("uploads"));文件未上传到系统。所以现在有人可以告诉我如果我不在服务器上上传文件如何获取文件的 byte[]。

最佳答案

FileUpload 对象为您提供上传文件的路径。您可以使用 Vert.x FileSystem API 加载其内容:

vertx.fileSystem().readFile(fileUpload.uploadedFileName(), ar -> {
if (ar.succeeded()) {
byte[] content = ar.result().getBytes();
// use the content
} else {
// deal with failure
}
});

关于java - 如何从vertx中的文件中获取byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50843766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com