Top Description Inners Fields Constructors Methods
org.eclipse.jetty.http

public Class MultiPartFormData

extends Object
Class Inheritance
Imports
java.io.Closeable, java.nio.ByteBuffer, java.nio.channels.NonWritableChannelException, .SeekableByteChannel, java.nio.charset.Charset, java.nio.file.Files, .Path, .StandardOpenOption, java.util.ArrayList, .Iterator, .List, .Objects, java.util.concurrent.CompletableFuture, java.util.function.Function, org.eclipse.jetty.io.Content, org.eclipse.jetty.io.content.ContentSourceCompletableFuture, org.eclipse.jetty.util.Attributes, .IO, org.eclipse.jetty.util.thread.AutoLock, org.slf4j.Logger, .LoggerFactory

A CompletableFuture that is completed when a multipart/form-data content has been parsed asynchronously from a Content.Source.

Once the parsing of the multipart/form-data content completes successfully, objects of this class are completed with a Parts object.

Objects of this class may be configured to save multipart files in a configurable directory, and configure the max size of such files, etc.

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.
MultiPartFormData.Parser formData = new MultiPartFormData.Parser(boundary);
// Where to store the files.
formData.setFilesDirectory(Path.of("/tmp"));
// Max 1 MiB files.
formData.setMaxFileSize(1024 * 1024);

// Parse the content.
formData.parse(content)
    // When complete, use the parts.
    .thenAccept(parts -> ...);
See Also
Parts

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
MultiPartFormData.ContentSource

The multipart/form-data specific content source.

public static class
public static class
MultiPartFormData.Parts

An ordered list of MultiPart.Parts that can be accessed by index or by name, or iterated over.

Field Summary

Modifier and TypeField and Description
private static final Logger

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static CompletableFuture<MultiPartFormData.Parts>
public static CompletableFuture<MultiPartFormData.Parts>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

LOGback to summary
private static final Logger LOG

Constructor Detail

MultiPartFormDataback to summary
private MultiPartFormData()

Method Detail

fromback to summary
public static CompletableFuture<MultiPartFormData.Parts> from(Attributes attributes, String boundary, Function<MultiPartFormData.Parser, CompletableFuture<MultiPartFormData.Parts>> parse)

Deprecated

for removal since 12.0.6.

use from(Attributes, ComplianceViolation.Listener, String, Function) instead. This method will be removed in Jetty 12.1.0

Annotations
@Deprecated
since:12.0.6
forRemoval:true
fromback to summary
public static CompletableFuture<MultiPartFormData.Parts> from(Attributes attributes, ComplianceViolation.Listener listener, String boundary, Function<MultiPartFormData.Parser, CompletableFuture<MultiPartFormData.Parts>> parse)
org.eclipse.jetty.http back to summary

public Class MultiPartFormData.ContentSource

extends AbstractContentSource
Class Inheritance

The multipart/form-data specific content source.

See Also
MultiPart.AbstractContentSource

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected HttpFields
Inherited from org.eclipse.jetty.http.MultiPart.AbstractContentSource:
addPartclosedemandfailgetBoundarygetLengthgetPartHeadersMaxLengthreadsetPartHeadersMaxLength

Constructor Detail

ContentSourceback to summary
public ContentSource(String boundary)

Method Detail

customizePartHeadersback to summary
protected HttpFields customizePartHeaders(MultiPart.Part part)

Overrides org.eclipse.jetty.http.MultiPart.AbstractContentSource.customizePartHeaders.

org.eclipse.jetty.http back to summary

public Class MultiPartFormData.Parser

extends Object
Class Inheritance

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private ComplianceViolation.Listener
private Path
private long
private final MultiPartFormData.Parser.PartsListener
private long
private long
private long
private final MultiPart.Parser
private MultiPartFormData.Parts
private boolean

Constructor Summary

AccessConstructor and Description
public
Parser(String boundary)

public
Parser(String boundary, ComplianceViolation.Listener complianceViolationListener)

Method Summary

Modifier and TypeMethod and Description
private Path
public String
getBoundary()

Returns: the boundary string

public Charset

Returns:

the default charset specified by the _charset_ part, or null if that part is not present
getDefaultCharset
()

Returns the default charset as specified by RFC 7578, section 4.6, that is the charset specified by the part named _charset_.

public Path
getFilesDirectory()

Returns: the directory where files are saved

public long
getMaxFileSize()

Returns: the maximum file size in bytes, or -1 for unlimited file size

public long
getMaxLength()

Returns: the maximum length in bytes of the whole multipart content, or -1 for unlimited length

public long
getMaxMemoryFileSize()

Returns: the maximum memory file size in bytes, or -1 for unlimited memory file size

public long
getMaxParts()

Returns: the maximum number of parts that can be parsed from the multipart content.

public int
getPartHeadersMaxLength()

Returns: the max length of a MultiPart.Part headers, in bytes, or -1 for unlimited length

pack-priv int
public boolean
isUseFilesForPartsWithoutFileName()

Returns: whether parts without fileName may be stored as files

public CompletableFuture<MultiPartFormData.Parts>
parse(Content.Source content)

public void
setFilesDirectory(Path
the directory where files are saved
filesDirectory
)

Sets the directory where the files uploaded in the parts will be saved.

public void
setMaxFileSize(long
the maximum file size in bytes, or -1 for unlimited file size
maxFileSize
)

public void
setMaxLength(long
the maximum length in bytes of the whole multipart content, or -1 for unlimited length
maxLength
)

public void
setMaxMemoryFileSize(long
the maximum memory file size in bytes, or -1 for unlimited memory file size
maxMemoryFileSize
)

Sets the maximum memory file size in bytes, after which files will be saved in the directory specified by setFilesDirectory(Path).

public void
setMaxParts(long
the maximum number of parts that can be parsed from the multipart content.
maxParts
)

public void
setPartHeadersMaxLength(int
the max length of a MultiPart.Part headers, in bytes, or -1 for unlimited length
partHeadersMaxLength
)

public void
setUseFilesForPartsWithoutFileName(boolean
whether parts without fileName may be stored as files
useFilesForPartsWithoutFileName
)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

complianceViolationListenerback to summary
private ComplianceViolation.Listener complianceViolationListener
filesDirectoryback to summary
private Path filesDirectory
lengthback to summary
private long length
listenerback to summary
private final MultiPartFormData.Parser.PartsListener listener
maxFileSizeback to summary
private long maxFileSize
maxLengthback to summary
private long maxLength
maxMemoryFileSizeback to summary
private long maxMemoryFileSize
parserback to summary
private final MultiPart.Parser parser
partsback to summary
private MultiPartFormData.Parts parts
useFilesForPartsWithoutFileNameback to summary
private boolean useFilesForPartsWithoutFileName

Constructor Detail

Parserback to summary
public Parser(String boundary)
Parserback to summary
public Parser(String boundary, ComplianceViolation.Listener complianceViolationListener)

Method Detail

findFilesDirectoryback to summary
private Path findFilesDirectory()
getBoundaryback to summary
public String getBoundary()
Returns:String

the boundary string

getDefaultCharsetback to summary
public Charset getDefaultCharset()

Returns the default charset as specified by RFC 7578, section 4.6, that is the charset specified by the part named _charset_.

If that part is not present, returns null.

Returns:Charset

the default charset specified by the _charset_ part, or null if that part is not present

getFilesDirectoryback to summary
public Path getFilesDirectory()
Returns:Path

the directory where files are saved

getMaxFileSizeback to summary
public long getMaxFileSize()
Returns:long

the maximum file size in bytes, or -1 for unlimited file size

getMaxLengthback to summary
public long getMaxLength()
Returns:long

the maximum length in bytes of the whole multipart content, or -1 for unlimited length

getMaxMemoryFileSizeback to summary
public long getMaxMemoryFileSize()
Returns:long

the maximum memory file size in bytes, or -1 for unlimited memory file size

getMaxPartsback to summary
public long getMaxParts()
Returns:long

the maximum number of parts that can be parsed from the multipart content.

getPartHeadersMaxLengthback to summary
public int getPartHeadersMaxLength()
Returns:int

the max length of a MultiPart.Part headers, in bytes, or -1 for unlimited length

getPartsSizeback to summary
pack-priv int getPartsSize()
isUseFilesForPartsWithoutFileNameback to summary
public boolean isUseFilesForPartsWithoutFileName()
Returns:boolean

whether parts without fileName may be stored as files

parseback to summary
public CompletableFuture<MultiPartFormData.Parts> parse(Content.Source content)
setFilesDirectoryback to summary
public void setFilesDirectory(Path filesDirectory)

Sets the directory where the files uploaded in the parts will be saved.

Parameters
filesDirectory:Path

the directory where files are saved

setMaxFileSizeback to summary
public void setMaxFileSize(long maxFileSize)
Parameters
maxFileSize:long

the maximum file size in bytes, or -1 for unlimited file size

setMaxLengthback to summary
public void setMaxLength(long maxLength)
Parameters
maxLength:long

the maximum length in bytes of the whole multipart content, or -1 for unlimited length

setMaxMemoryFileSizeback to summary
public void setMaxMemoryFileSize(long maxMemoryFileSize)

Sets the maximum memory file size in bytes, after which files will be saved in the directory specified by setFilesDirectory(Path).

Use value 0 to always save the files in the directory.

Use value -1 to never save the files in the directory.

Parameters
maxMemoryFileSize:long

the maximum memory file size in bytes, or -1 for unlimited memory file size

setMaxPartsback to summary
public void setMaxParts(long maxParts)
Parameters
maxParts:long

the maximum number of parts that can be parsed from the multipart content.

setPartHeadersMaxLengthback to summary
public void setPartHeadersMaxLength(int partHeadersMaxLength)
Parameters
partHeadersMaxLength:int

the max length of a MultiPart.Part headers, in bytes, or -1 for unlimited length

setUseFilesForPartsWithoutFileNameback to summary
public void setUseFilesForPartsWithoutFileName(boolean useFilesForPartsWithoutFileName)
Parameters
useFilesForPartsWithoutFileName:boolean

whether parts without fileName may be stored as files

org.eclipse.jetty.http back to summary

private Class MultiPartFormData.Parser.PartsListener

extends AbstractPartsListener
Class Inheritance

Field Summary

Modifier and TypeField and Description
private Throwable
private SeekableByteChannel
private Path
private long
private final AutoLock
private long
private final List<Content.Chunk>
private final List<MultiPart.Part>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private void
private void
private void
private boolean
private void
fail(Throwable cause)

private SeekableByteChannel
pack-priv Charset
pack-priv int
private boolean
public void
onComplete()

Overrides default org.eclipse.jetty.http.MultiPart.Parser.Listener.onComplete.

Callback method invoked when the whole multipart content has been parsed.

public void
onFailure(Throwable
the failure cause
failure
)

Overrides default org.eclipse.jetty.http.MultiPart.Parser.Listener.onFailure.

Callback method invoked when the parser cannot parse the multipart content.

public void
onPart(String
the part name
name
,
String
the part fileName
fileName
,
HttpFields
the part headers
headers
)

Implements abstract org.eclipse.jetty.http.MultiPart.AbstractPartsListener.onPart.

Callback method invoked when a Part has been parsed.

public void
onPartContent(Content.Chunk
the part content chunk
chunk
)

Overrides default org.eclipse.jetty.http.MultiPart.Parser.Listener.onPartContent.

Callback method invoked when a part content Chunk has been parsed.

private void
write(ByteBuffer buffer)

Inherited from org.eclipse.jetty.http.MultiPart.AbstractPartsListener:
getFileNamegetNameonPartEndonPartHeader

Field Detail

failureback to summary
private Throwable failure
fileChannelback to summary
private SeekableByteChannel fileChannel
filePathback to summary
private Path filePath
fileSizeback to summary
private long fileSize
lockback to summary
private final AutoLock lock
memoryFileSizeback to summary
private long memoryFileSize
partChunksback to summary
private final List<Content.Chunk> partChunks
partsback to summary
private final List<MultiPart.Part> parts

Constructor Detail

PartsListenerback to summary
private PartsListener()

Method Detail

closeback to summary
private void close()
createFileChannelback to summary
private void createFileChannel()
deleteback to summary
private void delete()
ensureFileChannelback to summary
private boolean ensureFileChannel()
failback to summary
private void fail(Throwable cause)
fileChannelback to summary
private SeekableByteChannel fileChannel()
getDefaultCharsetback to summary
pack-priv Charset getDefaultCharset()
getPartsSizeback to summary
pack-priv int getPartsSize()
isFailedback to summary
private boolean isFailed()
onCompleteback to summary
public void onComplete()

Overrides default org.eclipse.jetty.http.MultiPart.Parser.Listener.onComplete.

Doc from org.eclipse.jetty.http.MultiPart.Parser.Listener.onComplete.

Callback method invoked when the whole multipart content has been parsed.

Annotations
@Override
onFailureback to summary
public void onFailure(Throwable failure)

Overrides default org.eclipse.jetty.http.MultiPart.Parser.Listener.onFailure.

Doc from org.eclipse.jetty.http.MultiPart.Parser.Listener.onFailure.

Callback method invoked when the parser cannot parse the multipart content.

Parameters
failure:Throwable

the failure cause

Annotations
@Override
onPartback to summary
public void onPart(String name, String fileName, HttpFields headers)

Implements abstract org.eclipse.jetty.http.MultiPart.AbstractPartsListener.onPart.

Doc from org.eclipse.jetty.http.MultiPart.AbstractPartsListener.onPart.

Callback method invoked when a Part has been parsed.

Parameters
name:String

the part name

fileName:String

the part fileName

headers:HttpFields

the part headers

Annotations
@Override
onPartContentback to summary
public void onPartContent(Content.Chunk chunk)

Overrides default org.eclipse.jetty.http.MultiPart.Parser.Listener.onPartContent.

Doc from org.eclipse.jetty.http.MultiPart.Parser.Listener.onPartContent.

Callback method invoked when a part content Chunk has been parsed.

The Chunk must be Content.Chunk#retain() retained} if it not consumed by this method (for example, stored away for later use).

Parameters
chunk:Content.Chunk

the part content chunk

Annotations
@Override
writeback to summary
private void write(ByteBuffer buffer) throws Exception
org.eclipse.jetty.http back to summary

public Class MultiPartFormData.Parts

extends Object
implements Iterable<MultiPart.Part>, Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable, java.lang.Iterable

An ordered list of MultiPart.Parts that can be accessed by index or by name, or iterated over.

Field Summary

Modifier and TypeField and Description
private final List<MultiPart.Part>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.

public MultiPart.Part

Returns:

the MultiPart.Part at the given index
get
(int
the index of the MultiPart.Part to return
index
)

Returns the MultiPart.Part at the given index, a number between 0 included and the value returned by size() excluded.

public List<MultiPart.Part>

Returns:

all the MultiPart.Parts with the given name
getAll
(String
the MultiPart.Parts name
name
)

Returns all the MultiPart.Parts with the given name.

public MultiPart.Part

Returns:

the first MultiPart.Part with the given name, or null
getFirst
(String
the MultiPart.Part name
name
)

Returns the first MultiPart.Part with the given name, or null if no MultiPart.Part with that name exists.

public Iterator<MultiPart.Part>
iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

public int
size()

Returns: the number of parts

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

partsback to summary
private final List<MultiPart.Part> parts

Constructor Detail

Partsback to summary
private Parts(List<MultiPart.Part> parts)

Method Detail

closeback to summary
public void close()

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
getback to summary
public MultiPart.Part get(int index)

Returns the MultiPart.Part at the given index, a number between 0 included and the value returned by size() excluded.

Parameters
index:int

the index of the MultiPart.Part to return

Returns:MultiPart.Part

the MultiPart.Part at the given index

getAllback to summary
public List<MultiPart.Part> getAll(String name)

Returns all the MultiPart.Parts with the given name.

Parameters
name:String

the MultiPart.Parts name

Returns:List<MultiPart.Part>

all the MultiPart.Parts with the given name

getFirstback to summary
public MultiPart.Part getFirst(String name)

Returns the first MultiPart.Part with the given name, or null if no MultiPart.Part with that name exists.

Parameters
name:String

the MultiPart.Part name

Returns:MultiPart.Part

the first MultiPart.Part with the given name, or null

iteratorback to summary
public Iterator<MultiPart.Part> iterator()

Implements java.lang.Iterable.iterator.

Doc from java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

Returns:Iterator<MultiPart.Part>

an Iterator.

Annotations
@Override
sizeback to summary
public int size()
Returns:int

the number of parts

See Also
get(int)