简体   繁体   English

使用 Undertow 的多部分表单数据示例

[英]Multipart form-data example using Undertow

I'm trying to upload a text file from a html form.我正在尝试从 html 表单上传文本文件。

Is there any example on how to get the text-file from the HttpHandler有没有关于如何从HttpHandler获取文本文件的例子

I once used the following code:我曾经使用过以下代码:

    Builder builder = FormParserFactory.builder();

    final FormDataParser formDataParser = builder.build().createParser(exchange);
    if (formDataParser != null) {
        exchange.startBlocking();
        FormData formData = formDataParser.parseBlocking();

        for (String data : formData) {
            for (FormData.FormValue formValue : formData.get(data)) {
                if (formValue.isFile()) {
                    // process file here: formValue.getFile();
                } 
            }
        }
    }

Based on: http://www.programcreek.com/java-api-examples/index.php?api=io.undertow.server.handlers.form.FormData基于: http : //www.programcreek.com/java-api-examples/index.php?api=io.undertow.server.handlers.form.FormData

You can use the built-in EagerFormParsingHandler and chain your handler, as in the example below.您可以使用内置的EagerFormParsingHandler并链接您的处理程序,如下例所示。 This handler will parse the request and store the multi-part file(s) to your "java.io.tmpdir" system property defined directory (by default, but is configurable).此处理程序将解析请求并将多部分文件存储到您的“java.io.tmpdir”系统属性定义目录(默认情况下,但可配置)。 In your handle, you can find the file and process it as you want.在您的句柄中,您可以找到该文件并根据需要对其进行处理。 Additionally, EagerFormParsingHandler adds a listener in order to delete any created files from your file system, as soon as the exchange completes.此外, EagerFormParsingHandler添加了一个侦听器,以便在交换完成后立即从文件系统中删除任何创建的文件。

    HttpHandler multipartProcessorHandler = (exchange) -> {
        FormData attachment = exchange.getAttachment(FormDataParser.FORM_DATA);
        FormData.FormValue fileValue = attachment.get("file").getFirst();
        Path file = fileValue.getPath();
    };

    Undertow server = Undertow.builder()
        .addHttpListener(8080, "localhost")
        .setHandler(
            new EagerFormParsingHandler(
                FormParserFactory.builder()
                    .addParsers(new MultiPartParserDefinition())
                    .build()
            ).setNext(multipartProcessorHandler)
        )
        .build();
    server.start();

This is what I did:这就是我所做的:

public class HttpServer{

    public void start() throws IOException{

        Undertow server = Undertow.builder()
            .addHttpListener(8080, "0.0.0.0")
            .setHandler(new HttpHandler() {
                @Override
                public void handleRequest(HttpServerExchange exchange) throws Exception {
                    // Parses HTTP POST form data and passes it to a handler asynchronously 
                    FormDataParser parser = FormParserFactory.builder().build().createParser(exchange);
                    MyHandler handler = new MyHandler();
                    parser.parse(handler);
                }
            }).build();

        server.start();

    }

    private class MyHandler implements HttpHandler{
        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            // Form data is stored here
            FormData formData = exchange.getAttachment(FormDataParser.FORM_DATA);
            // Iterate through form data
            for (String data : formData) {
                for (FormData.FormValue formValue : formData.get(data)) {
                    if (formValue.isFileItem()) {
                        // Process file here
                        File uploadedFile = formValue.getFileItem().getFile().toFile();
                    } 
                }
            }
        }
    }
}

From the documentation:从文档:

void parse(HttpHandler next) throws Exception void parse(HttpHandler next) 抛出异常

Parse the form data asynchronously.异步解析表单数据。 If all the data cannot be read immediately then a read listener will be registered, and the data will be parsed by the read thread.如果不能立即读取所有数据,则将注册读取侦听器,并由读取线程解析数据。 When this method completes the handler will be invoked, and the data will be attached under FORM_DATA.当此方法完成时,将调用处理程序,并将数据附加到 FORM_DATA 下。

The method can either invoke the next handler directly, or may delegate to the IO thread to perform the parsing.该方法可以直接调用下一个处理程序,也可以委托 IO 线程执行解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM