作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从一个文件中提取 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/
我是一名优秀的程序员,十分优秀!