Class MultiPartFormData

java.lang.Object
org.eclipse.jetty.http.MultiPartFormData

public class MultiPartFormData extends Object

A container class for multipart/form-data parsing and generation.

Use MultiPartFormData.Parser to parse multipart/form-data, and use MultiPartFormData.ContentSource to generate multipart/form-data from parts.

Once the parsing of the multipart/form-data content completes successfully, a MultiPartFormData.Parts object is provided.

MultiPartFormData.Parser may be configured to save multipart files in a configurable directory, and configure the max size of such files, etc. via MultiPartConfig.

Typical usage:

// Some headers that include Content-Type.
HttpFields headers = ...;
String boundary = MultiPart.extractBoundary(headers.get(HttpHeader.CONTENT_TYPE));

// Some multipart/form-data content.
Content.Source content = ...;

// Create and configure MultiPartFormData.Parser.
MultiPartFormData.Parser parser = new MultiPartFormData.Parser(boundary);
// Where to store the files.
parser.setFilesDirectory(Path.of("/tmp"));
// Max 1 MiB files.
parser.setMaxFileSize(1024 * 1024);

// Parse the content.
parser.parse(content, new Promise.Invocable<>()
{
    @Override
    public void succeeded(MultiPartFormData.Parts parts)
    {
        // Use the parts.
    }

    @Override
    public void failed(Throwable x)
    {
        // Handle failure.
    }
}

For usage within a server environment, use:

Request request = ...;
String contentType = request.getHeaders().get("Content-Type");
MultiPartConfig config = new MultiPartConfig.Builder()
    .location(Path.of("/tmp"))
    .maxPartSize(1024 * 1024)
    .build();
MultiPartFormData.onParts(request, request, contentType, config, new Promise.Invocable<>()
{
    @Override
    public void succeeded(MultiPartFormData.Parts parts)
    {
        // Use the parts.
    }

    @Override
    public void failed(Throwable x)
    {
        // Handle failure.
    }
});
See Also: