首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Vertx3.0简单表单上传

Vertx3.0简单表单上传
EN

Stack Overflow用户
提问于 2016-02-25 06:24:35
回答 2查看 1.4K关注 0票数 0

Vertx3.0 http simpleform文件上载程序正在为多个文件抛出错误。

我使用vertx3.0简单的表单上传。当我上传单个文件时,它工作得很好。如果表单的输入是" multiple“,并选择了多个文件,则HTTPServerUpload会抛出错误"Response has written”。由于响应在第一个文件的结束处理程序中结束,因此它将在后续文件中抛出此错误。有没有其他方法可以处理多个文件?

使用vertx3.0上传Simpleform文件

代码语言:javascript
运行
复制
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>
EN

回答 2

Stack Overflow用户

发布于 2016-02-25 21:32:29

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

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2016-09-16 17:28:53

vert.x中可以实现多个文件上传。在HTML中使用多个upload按钮,并使用HttpRequestuploadHandler。上载任何数量的文件时,UploadHandler都会被调用多次。

代码语言:javascript
运行
复制
    HttpServerRequest request = routingContext.request();
    request.setExpectMultipart(true);
    request.endHandler(new Handler<Void>() {
        @Override
        public void handle(Void aVoid) {
            MultiMap entries = request.formAttributes();
            Set<String> names = entries.names();
            logger.info("UPLOAD_CONTENT: fileName = "+entries.get("fileName"));
            logger.info("UPLOAD_CONTENT: type = "+entries.get("type"));
            logger.info("UPLOAD_CONTENT: names = "+names);
            request.response().setChunked(true).end(createResponse("SUCCESS"));
        }
    });
    // This would be called multiple times
    request.uploadHandler(upload -> {

        upload.exceptionHandler(new Handler<Throwable>() {
            @Override
            public void handle(Throwable error) {
                logger.error("UPLOAD_CONTENT: Error while uploading content "+upload.filename());
                logger.error("UPLOAD_CONTENT: error = "+error.toString());
                error.printStackTrace();
                request.response().setChunked(true).end(createResponse("FAILURE"));
            }
        });
        upload.endHandler(new Handler<Void>() {
            @Override
            public void handle(Void aVoid) {
                logger.info("UPLOAD_CONTENT: fileName = "+upload.filename());
                logger.info("UPLOAD_CONTENT: name = "+upload.name());
                logger.info("UPLOAD_CONTENT: contentType = "+upload.contentType());
                logger.info("UPLOAD_CONTENT: size = "+upload.size());
                UtilityFunctions.uploadToS3(upload.filename(), "testfolder");

            }
        });
        upload.streamToFileSystem(upload.filename());
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35614439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档