EntityPart is one part of a multipart entity. As defined in
RFC 7578, a multipart
request or response must have a content type of "multipart/form-data" with a
boundary parameter indicating where one part ends the next may begin.
Multipart entities may be received in a resource method as a collection of
parts (e.g. List<EntityPart>) or as a form parameter (ex:
@FormParam("part1Name") EntityPart part1).
Likewise, a client may receive a multipart response by reading the returned
entity as a collection of EntityParts (ex: response.readEntity(new
GenericType<List<EntityPart>>() {})).
In order to send a multipart entity either as a client request or a response
from a resource method, you may create the Lists using
EntityPart.Builder. For example:
Client c = ClientBuilder.newClient();
WebTarget target = c.target(someURL);
List<EntityPart> parts = Arrays.asList(
EntityPart.withName("name1").fileName("file1.doc").content(stream1).build(),
EntityPart.withName("name2").fileName("file2.doc").content(stream2).build(),
EntityPart.withName("name3").fileName("file3.xml").content(myObject, MyClass.class).mediaType("application/xml").build());
GenericEntity<List<EntityPart>> genericEntity = new GenericEntity<>(parts){};
Entity entity = Entity.entity(genericEntity, MediaType.MULTIPART_FORM_DATA);
Response r = target.request().post(entity);
Note that when building a EntityPart, the name and content are required.
Other properties such as headers, file name, and media type are optional.
It is the responsibility of the implementation code to close the content
input streams when sending the multipart content. Closing the stream before
the implementation has sent it could result in unexpected exceptions. It is
the responsibility of the calling code to close the stream when receiving the
multipart content.
| Modifier and Type | Class and Description |
|---|---|
| public static interface | EntityPart.Builder
Builder for |
| Modifier and Type | Method and Description |
|---|---|
| public InputStream | Returns: anInputStream representing the content of this partReturns the input stream for this part. |
| public < the entity type T> T | Returns: an instance of the specifiedClass representing the content
of this partthe type)Class that the implementation should convert this
part toConverts the content stream for this part to the specified class and returns it. |
| public < the entity type T> T | Returns: an instance of the specified generic type representing the content of this partthe generic type that the implementation should convert this part
to type)Converts the content stream for this part to the specified type and returns it. |
| public Optional | Returns: anOptional<String> indicating the filename if presentReturns the filename of this part. |
| public MultivaluedMap | Returns: immutableMultivaluedMap<String, String> of part headersReturns an immutable multivalued map of headers for this specific part. |
| public MediaType | Returns: the media type for this partReturns the content type of this part, and equivalent to calling
|
| public String | |
| public static EntityPart. | withFileName(String
name and filename of the part to create within the
multipart entity partAndFileName)Creates a new |
| public static EntityPart. |
| getContent | back to summary |
|---|---|
| public InputStream getContent() Returns the input stream for this part. This is the content body of the part and is accessed as a stream to avoid loading potentially large amounts of data into the heap. It is the responsibility of the calling code to close this stream after receiving it.
| |
| getContent | back to summary |
|---|---|
| public <T> T getContent(Class<T> type) throws IllegalArgumentException, IllegalStateException, IOException, WebApplicationException Converts the content stream for this part to the specified class and returns
it. The implementation must convert the stream by finding a
| |
| getContent | back to summary |
|---|---|
| public <T> T getContent(GenericType<T> type) throws IllegalArgumentException, IllegalStateException, IOException, WebApplicationException Converts the content stream for this part to the specified type and returns
it. The implementation must convert the stream by finding a
| |
| getFileName | back to summary |
|---|---|
| public Optional Returns the filename of this part. This will be the "filename" attribute of
the | |
| getHeaders | back to summary |
|---|---|
| public MultivaluedMap Returns an immutable multivalued map of headers for this specific part.
| |
| getMediaType | back to summary |
|---|---|
| public MediaType getMediaType() Returns the content type of this part, and equivalent to calling
| |
| getName | back to summary |
|---|---|
| public String getName() Returns the name of this part within the multipart entity. This will be the
"name" attribute of the
| |
| withFileName | back to summary |
|---|---|
| public static EntityPart. Creates a new
Logically, this is the same as
| |
| withName | back to summary |
|---|---|
| public static EntityPart. Creates a new
| |
EntityPart instances.
| Modifier and Type | Method and Description |
|---|---|
| public EntityPart | Returns: EntityPart instance built from the provided property values.Builds a new EntityPart instance using the provided property values. |
| public EntityPart. | Returns: the updated builderInputStream of the content of this partSets the content for this part. |
| public default EntityPart. | Returns: the updated builder.the filename of the part. fileName, InputStream the content stream of the part. content)Convenience method, equivalent to calling
|
| public < the entity type T> EntityPart. | Returns: the updated builder.the object to be used as the content content, Class<? extends T> the type of this object which will be used when selecting the
appropriate type)jakarta.ws.rs.ext.MessageBodyWriterSets the content for this part. |
| public default EntityPart. | |
| public < the entity type T> EntityPart. | Returns: the updated builder.the object to be used as the content content, GenericType<T> the generic type of this object which will be used when
selecting the appropriate
type)jakarta.ws.rs.ext.MessageBodyWriterSets the content for this part. |
| public EntityPart. | |
| public EntityPart. | |
| public EntityPart. | Returns: the updated builderthe multivalued map of headers to add to this part newHeaders)Adds new headers or replaces previously added headers. |
| public EntityPart. | |
| public EntityPart. |
| build | back to summary |
|---|---|
| public EntityPart build() throws IllegalStateException, IOException, WebApplicationException Builds a new EntityPart instance using the provided property values.
| |
| content | back to summary |
|---|---|
| public EntityPart. Sets the content for this part. The content of this builder must be specified
before invoking the
The
| |
| content | back to summary |
|---|---|
| public default EntityPart. Convenience method, equivalent to calling
| |
| content | back to summary |
|---|---|
| public <T> EntityPart. Sets the content for this part. The content of this builder must be specified
before invoking the
If the content is specified using this method, then the
| |
| content | back to summary |
|---|---|
| public default EntityPart. Sets the content for this part. The content of this builder must be specified
before invoking the
If the content is specified using this method, then the
This is the equivalent of calling
| |
| content | back to summary |
|---|---|
| public <T> EntityPart. Sets the content for this part. The content of this builder must be specified
before invoking the
If the content is specified using this method, then the
| |
| fileName | back to summary |
|---|---|
| public EntityPart. Sets the file name for this part. The file name will be specified as an
attribute in the
| |
| header | back to summary |
|---|---|
| public EntityPart. Adds a new header or replaces a previously added header and sets the header value(s).
| |
| headers | back to summary |
|---|---|
| public EntityPart. Adds new headers or replaces previously added headers. The behavior of this
method would be the same as if iterating over the entry set and invoking the
| |
| mediaType | back to summary |
|---|---|
| public EntityPart. Sets the media type for the EntityPart. This will also set the
| |
| mediaType | back to summary |
|---|---|
| public EntityPart. Convenience method for setting the media type for the EntityPart. This will
also set the
| |