Vertx3.0简单表单上传

时间:2016-02-24 22:24:35

标签: vert.x

Vertx3.0 http simpleform文件上传器为多个文件抛出错误。

使用vertx3.0简单表单上传。我上传单个文件时工作正常。如果表单有输入"多个"并选择多个文件,HTTPServerUpload抛出错误"响应已写入"。由于响应在第一个文件的endhandler中结束,因此它会为后续文件抛出此错误。多个文件还有其他方法吗?

使用vertx3.0上传Simpleform文件

public class SimpleFormUploadServer extends AbstractVerticle {


    public static void main(String[] args) {
        Runner.runExample(SimpleFormUploadServer.class);
    }

    @Override
    public void start() throws Exception {
        vertx.createHttpServer()
                .requestHandler(req -> {
                    if (req.uri().equals("/")) {
                        // Serve the index page
                        req.response().sendFile("index.html");
                    } else if (req.uri().startsWith("/form")) {
                        req.setExpectMultipart(true);
                        req.uploadHandler(upload -> {
                            upload.exceptionHandler(cause -> {
                                req.response().setChunked(true)
                                        .end("Upload failed");
                            });

                            upload.endHandler(v -> {
                                req.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());
                        });
                    } else {
                        req.response().setStatusCode(404);
                        req.response().end();
                    }
                }).listen(8080);

    }
}

Exception :

SEVERE: Unhandled exception
java.lang.IllegalStateException: Response has already been written
    at io.vertx.core.http.impl.HttpServerResponseImpl.checkWritten(HttpServerResponseImpl.java:561)
    at io.vertx.core.http.impl.HttpServerResponseImpl.end0(HttpServerResponseImpl.java:389)
    at io.vertx.core.http.impl.HttpServerResponseImpl.end(HttpServerResponseImpl.java:307)
    at io.vertx.core.http.impl.HttpServerResponseImpl.end(HttpServerResponseImpl.java:292)
    at com.nokia.doas.vertx.http.upload.SimpleFormUploadServer$1$1$2.handle(SimpleFormUploadServer.java:85)
    at com.nokia.doas.vertx.http.upload.SimpleFormUploadServer$1$1$2.handle(SimpleFormUploadServer.java:1)
    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)


index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
</head>
<body>

<form action="/form" ENCTYPE="multipart/form-data" method="POST" name="wibble">
    choose a file to upload:<input type="file"  name="files" multiple="multiple"/><br>
    <input type="submit"/>
</form>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

您可以使用vertx-web轻松处理文件上传:

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

router.post("/some/path/uploads").handler(routingContext -> {

    Set<FileUpload> uploads = routingContext.fileUploads();
    // Do something with uploads....
});

此外,您将受益routing facility,甚至可以serve static files,例如index.html。

希望这会有所帮助。

答案 1 :(得分:0)

vert.x可以实现多个文件上传。在HTML中使用多个上传按钮,并使用uploadHandler的{​​{1}}。任意多个文件上传的次数都会被调用HttpRequest

UploadHandler