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

Multipart form-data example using Undertow

我正在尝试从 html 表单上传文本文件。

是否有关于如何从 HttpHandler

获取文本文件的示例

我曾经使用过下面的代码:

    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();
                } 
            }
        }
    }

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

您可以使用内置的 EagerFormParsingHandler 并链接您的处理程序,如下例所示。此处理程序将解析请求并将多部分文件存储到您的 "java.io.tmpdir" 系统 属性 定义的目录(默认情况下,但可配置)。在您的句柄中,您可以找到该文件并根据需要对其进行处理。此外,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();

这是我做的:

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();
                    } 
                }
            }
        }
    }
}

来自文档:

void parse(HttpHandler next) throws Exception

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.

The method can either invoke the next handler directly, or may delegate to the IO thread to perform the parsing.