gpt4 book ai didi

Java vertx 3.2 使用bodyhandler上传文件

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

java.lang.IllegalStateException: vertx3.0 中请求已被读取异常。我尝试了很多方法。简单的表单上传工作正常。但是当我使用主体处理程序时,它抛出以下异常。有人可以帮忙吗?

import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpHeaders;

import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;

/*
* @author Gogs
*/

public class TestServer extends AbstractVerticle {

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Runner.runExample(TestServer.class);
}

@Override
public void start() throws Exception {

Router router = Router.router(vertx);

// Enable multipart form data parsing
router.route().handler(BodyHandler.create());

router.route("/").handler(routingContext -> {
routingContext.response().putHeader("content-type", "text/html").end(
"<form action=\"/form\" ENCTYPE=\"multipart/form-data\" method=\"POST\" name=\"wibble\">\n" +
"choose a file to upload:<input type=\"file\" name=\"myfile\"/><br>"+
" <input type=\"submit\"/>"+
"</form>"
);
});

// handle the form
router.post("/form").handler(ctx -> {

ctx.request().setExpectMultipart(true);
ctx.request().uploadHandler(upload -> {
upload.exceptionHandler(cause -> {
ctx.response().setChunked(true).end("Upload failed");
});

upload.endHandler(v -> {
ctx.response().setChunked(true).end("Successfully uploaded to " + upload.filename());
});
// FIXME - Potential security exploit! In a real system you must check this filename
// to make sure you're not saving to a place where you don't want!
// Or better still, just use Vert.x-Web which controls the upload area.
upload.streamToFileSystem(upload.filename());
});


});

vertx.createHttpServer().requestHandler(router::accept).listen(8090);
}
}

Am seeing the below exception.


Feb 02, 2016 6:48:54 PM io.vertx.ext.web.impl.RoutingContextImplBase

SEVERE: Unexpected exception in route

java.lang.IllegalStateException: Request has already been read

at io.vertx.core.http.impl.HttpServerRequestImpl.checkEnded(HttpServerRequestImpl.java:426)
at io.vertx.core.http.impl.HttpServerRequestImpl.setExpectMultipart(HttpServerRequestImpl.java:322)
at io.vertx.ext.web.impl.HttpServerRequestWrapper.setExpectMultipart(HttpServerRequestWrapper.java:166)
at com.vertx.http.upload.TestServer.lambda$1(TestServer.java:43)
at io.vertx.ext.web.impl.RouteImpl.handleContext(RouteImpl.java:221)
at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:78)
at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:93)
at io.vertx.ext.web.handler.impl.BodyHandlerImpl$BHandler.doEnd(BodyHandlerImpl.java:155)
at io.vertx.ext.web.handler.impl.BodyHandlerImpl$BHandler.uploadEnded(BodyHandlerImpl.java:135)
at io.vertx.ext.web.handler.impl.BodyHandlerImpl$BHandler.lambda$null$35(BodyHandlerImpl.java:109)
at io.vertx.core.http.impl.HttpServerFileUploadImpl.notifyEndHandler(HttpServerFileUploadImpl.java:213)
at io.vertx.core.http.impl.HttpServerFileUploadImpl.lambda$handleComplete$165(HttpServerFileUploadImpl.java:206)
at io.vertx.core.file.impl.AsyncFileImpl.lambda$doClose$226(AsyncFileImpl.java:470)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$16(ContextImpl.java:335)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:358)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
at java.lang.Thread.run(Unknown Source)

最佳答案

您正在阅读请求正文两次。第一次读取是通过 BodyHandler (请参阅 BodyHanderImpl),第二次读取是通过您自己的处理程序(请参阅 HttpServerRequestImplHttpServerFileUploadImpl) >).

BodyHandler 从请求中完全读取正文并使其在 context.body 中可用。路由器的配置方式:

router.route().handler(BodyHandler.create());

将在路由器处理的每个请求上完整读取正文。

您的处理程序还完整读取请求正文并将其内容写入文件系统。您的路由器配置为仅在对 /form 进行 POST 时执行处理程序。

回顾一下流程,当您将上传提交到 /form 时,BodyHandler 正在完全读取请求,将内容存储在 context.body 中,并标记请求正文如所读。您的路由器将 uri 路径与您的上传处理程序相匹配,并尝试再次读取正文,但由于它已经被读取,因此会引发异常。

一些想法...

如果您的目的是将上传的文件写入文件系统,您实际上不需要在路由器中配置 BodyHandler。当您需要内存中的主体以便以某种方式处理它时,您可能需要使用 BodyHandler。除非您打算对路由器收到的单个请求执行处理程序,否则不应在没有匹配条件的情况下配置处理程序(即 router.route().handler(...))。这种类型的处理程序的一个很好的用例是 CookieHandler。

关于Java vertx 3.2 使用bodyhandler上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35166666/

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