Vishnu667
Vishnu667

Reputation: 768

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

Upvotes: 4

Views: 5273

Answers (3)

Pouria P
Pouria P

Reputation: 595

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

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.

Upvotes: 1

Ioannis Sermetziadis
Ioannis Sermetziadis

Reputation: 797

You can use the built-in EagerFormParsingHandler and chain your handler, as in the example below. 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). 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.

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

Upvotes: 4

mkobel
mkobel

Reputation: 346

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

Upvotes: 5

Related Questions