Search code examples
javaundertow

Multipart form-data example using Undertow


I'm trying to upload a text file from a html form.

Is there any example on how to get the text-file from the HttpHandler


Solution

  • 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