HttpMessageConverter to read and write 'normal' HTML
forms and also to write (but not read) multipart data (e.g. file uploads).
In other words, this converter can read and write the
"application/x-www-form-urlencoded" media type as
MultiValueMap<String, String>, and it can also
write (but not read) the "multipart/form-data" and
"multipart/mixed" media types as
MultiValueMap<String, Object>.
By default, "multipart/form-data" is used as the content type when
writing multipart data. As of Spring Framework 5.2 it is
also possible to write multipart data using other multipart subtypes such as
"multipart/mixed" and "multipart/related", as long as the
multipart subtype is registered as a supported media type and the desired multipart subtype is specified
as the content type when writing the multipart data. Note
that "multipart/mixed" is registered as a supported media type by
default.
When writing multipart data, this converter uses other
HttpMessageConverters to write the respective
MIME parts. By default, basic converters are registered for byte array,
String, and Resource. These can be overridden via
setPartConverters or augmented via addPartConverter.
The following snippet shows how to submit an HTML form using the
"multipart/form-data" content type.
RestTemplate restTemplate = new RestTemplate();
// AllEncompassingFormHttpMessageConverter is configured by default
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("field 1", "value 1");
form.add("field 2", "value 2");
form.add("field 2", "value 3");
form.add("field 3", 4); // non-String form values supported as of 5.1.4
restTemplate.postForLocation("https://example.com/myForm", form);
The following snippet shows how to do a file upload using the
"multipart/form-data" content type.
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
parts.add("field 1", "value 1");
parts.add("file", new ClassPathResource("myFile.jpg"));
restTemplate.postForLocation("https://example.com/myFileUpload", parts);
The following snippet shows how to do a file upload using the
"multipart/mixed" content type.
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
parts.add("field 1", "value 1");
parts.add("file", new ClassPathResource("myFile.jpg"));
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.MULTIPART_MIXED);
restTemplate.postForLocation("https://example.com/myFileUpload",
new HttpEntity<>(parts, requestHeaders));
The following snippet shows how to do a file upload using the
"multipart/related" content type.
MediaType multipartRelated = new MediaType("multipart", "related");
restTemplate.getMessageConverters().stream()
.filter(FormHttpMessageConverter.class::isInstance)
.map(FormHttpMessageConverter.class::cast)
.findFirst()
.orElseThrow(() -> new IllegalStateException("Failed to find FormHttpMessageConverter"))
.addSupportedMediaTypes(multipartRelated);
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
parts.add("field 1", "value 1");
parts.add("file", new ClassPathResource("myFile.jpg"));
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(multipartRelated);
restTemplate.postForLocation("https://example.com/myFileUpload",
new HttpEntity<>(parts, requestHeaders));
Some methods in this class were inspired by
org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity.
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter , org.springframework.util.MultiValueMap
| Modifier and Type | Class and Description |
|---|---|
| private static class | FormHttpMessageConverter.
Implementation of |
| private static class | FormHttpMessageConverter.
OutputStream that neither flushes nor closes. |
| Modifier and Type | Field and Description |
|---|---|
| private Charset | |
| public static final Charset | DEFAULT_CHARSET
The default charset used by the converter. |
| private static final MediaType | |
| private Charset | |
| private List | |
| private List |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | addSupportedMediaTypes(MediaType...
a var-args list of supportedMediaTypes)MediaType objects to addAdd |
| private void | |
| public boolean | canRead(Class<?>
the class to test for readability clazz, MediaType the media type to read (can be mediaType)null if not specified);
typically the value of a Content-Type header.Implements org. Indicates whether the given class can be read by this converter. |
| public boolean | canWrite(Class<?>
the class to test for writability clazz, MediaType the media type to write (can be mediaType)null if not specified);
typically the value of an Accept header.Implements org. Indicates whether the given class can be written by this converter. |
| protected byte[] | |
| protected String | Returns: the filename, ornull if not knownthe part to determine the file name for part)Return the filename of the given multipart part. |
| protected MediaType | Returns: the content type to be usedthe preferred content type (can be contentType)null)Return the content type used to write forms, given the preferred content type. |
| protected HttpEntity | Returns: the part Object itself it is anHttpEntity,
or a newly built HttpEntity wrapper for that partthe part to return an part)HttpEntity forReturn an |
| public List | |
| public List | getSupportedMediaTypes()
Implements org. Return the list of media types supported by this converter. |
| private boolean | isFilenameCharsetSet()
When |
| private boolean | |
| public MultiValueMap | read(Class<? extends MultiValueMap<String, ?>>
the type of object to return. This type must have previously been passed to the
clazz, HttpInputMessage canRead method of this interface, which must have returned true.the HTTP input message to read from inputMessage)Implements org. Read an object of the given type from the given input message, and returns it. |
| protected String | |
| public void | setCharset(Charset charset)
Set the default character set to use for reading and writing form data when
the request or response |
| public void | setMultipartCharset(Charset charset)
Set the character set to use when writing multipart data to encode file names. |
| public void | setPartConverters(List<HttpMessageConverter<?>> partConverters)
Set the message body converters to use. |
| public void | setSupportedMediaTypes(List<MediaType> supportedMediaTypes)
Set the list of |
| public void | write(MultiValueMap<String, ?>
the object to write to the output message. The type of this object must have previously been
passed to the map, MediaType canWrite method of this interface, which must have returned true.the content type to use when writing. May be contentType, HttpOutputMessage null to indicate that the
default content type of the converter must be used. If not null, this media type must have
previously been passed to the canWrite method of this interface, which must have
returned true.the message to write to outputMessage)Implements org. Write a given object to the given output message. |
| private void | |
| private static void | |
| private void | writeForm(MultiValueMap<String, Object> formData, MediaType contentType, HttpOutputMessage outputMessage)
|
| private void | writeMultipart(MultiValueMap<String, Object> parts, MediaType contentType, HttpOutputMessage outputMessage)
|
| private static void | |
| private void | |
| private void |
| charset | back to summary |
|---|---|
| private Charset charset | |
| DEFAULT_CHARSET | back to summary |
|---|---|
| public static final Charset DEFAULT_CHARSET The default charset used by the converter. | |
| DEFAULT_FORM_DATA_MEDIA_TYPE | back to summary |
|---|---|
| private static final MediaType DEFAULT_FORM_DATA_MEDIA_TYPE | |
| multipartCharset | back to summary |
|---|---|
| private Charset multipartCharset
| |
| partConverters | back to summary |
|---|---|
| private List<HttpMessageConverter<?>> partConverters | |
| supportedMediaTypes | back to summary |
|---|---|
| private List<MediaType> supportedMediaTypes | |
| FormHttpMessageConverter | back to summary |
|---|---|
| public FormHttpMessageConverter() | |
| addPartConverter | back to summary |
|---|---|
| public void addPartConverter(HttpMessageConverter<?> partConverter) Add a message body converter. Such a converter is used to convert objects to MIME parts. | |
| addSupportedMediaTypes | back to summary |
|---|---|
| public void addSupportedMediaTypes(MediaType... supportedMediaTypes) Add The supplied
| |
| applyDefaultCharset | back to summary |
|---|---|
| private void applyDefaultCharset() Apply the configured charset as a default to registered part converters. | |
| canRead | back to summary |
|---|---|
| public boolean canRead(Class<?> clazz, MediaType mediaType) Implements org. Doc from org. Indicates whether the given class can be read by this converter. | |
| canWrite | back to summary |
|---|---|
| public boolean canWrite(Class<?> clazz, MediaType mediaType) Implements org. Doc from org. Indicates whether the given class can be written by this converter. | |
| generateMultipartBoundary | back to summary |
|---|---|
| protected byte[] generateMultipartBoundary() Generate a multipart boundary. This implementation delegates to
| |
| getFilename | back to summary |
|---|---|
| protected String getFilename(Object part) Return the filename of the given multipart part. This value will be used for the
The default implementation returns | |
| getFormContentType | back to summary |
|---|---|
| protected MediaType getFormContentType(MediaType contentType) Return the content type used to write forms, given the preferred content type.
By default, this method returns the given content type, but adds the
charset if it does not have one.
If Subclasses can override this method to change this behavior. | |
| getHttpEntity | back to summary |
|---|---|
| protected HttpEntity Return an
| |
| getPartConverters | back to summary |
|---|---|
| public List Return the configured converters for MIME parts.
| |
| getSupportedMediaTypes | back to summary |
|---|---|
| public List Implements org. Doc from org. Return the list of media types supported by this converter. The list may
not apply to every possible target element type and calls to this method
should typically be guarded via
| |
| isFilenameCharsetSet | back to summary |
|---|---|
| private boolean isFilenameCharsetSet() When | |
| isMultipart | back to summary |
|---|---|
| private boolean isMultipart(MultiValueMap<String, ?> map, MediaType contentType) | |
| read | back to summary |
|---|---|
| public MultiValueMap Implements org. Doc from org. Read an object of the given type from the given input message, and returns it.
| |
| serializeForm | back to summary |
|---|---|
| protected String serializeForm(MultiValueMap<String, Object> formData, Charset charset) | |
| setCharset | back to summary |
|---|---|
| public void setCharset(Charset charset) Set the default character set to use for reading and writing form data when
the request or response As of 4.3, this is also used as the default charset for the conversion of text bodies in a multipart request. As of 5.0, this is also used for part headers including
By default this is set to "UTF-8". | |
| setMultipartCharset | back to summary |
|---|---|
| public void setMultipartCharset(Charset charset) Set the character set to use when writing multipart data to encode file
names. Encoding is based on the As of 5.0 by default part headers, including
| |
| setPartConverters | back to summary |
|---|---|
| public void setPartConverters(List<HttpMessageConverter<?>> partConverters) Set the message body converters to use. These converters are used to convert objects to MIME parts. | |
| setSupportedMediaTypes | back to summary |
|---|---|
| public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes) Set the list of | |
| write | back to summary |
|---|---|
| public void write(MultiValueMap<String, ?> map, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException Implements org. Doc from org. Write a given object to the given output message.
| |
| writeBoundary | back to summary |
|---|---|
| private void writeBoundary(OutputStream os, byte[] boundary) throws IOException | |
| writeEnd | back to summary |
|---|---|
| private static void writeEnd(OutputStream os, byte[] boundary) throws IOException | |
| writeForm | back to summary |
|---|---|
| private void writeForm(MultiValueMap<String, Object> formData, MediaType contentType, HttpOutputMessage outputMessage) throws IOException | |
| writeMultipart | back to summary |
|---|---|
| private void writeMultipart(MultiValueMap<String, Object> parts, MediaType contentType, HttpOutputMessage outputMessage) throws IOException | |
| writeNewLine | back to summary |
|---|---|
| private static void writeNewLine(OutputStream os) throws IOException | |
| writePart | back to summary |
|---|---|
private void writePart(String name, HttpEntity<?> partEntity, OutputStream os) throws IOException
| |
| writeParts | back to summary |
|---|---|
| private void writeParts(OutputStream os, MultiValueMap<String, Object> parts, byte[] boundary) throws IOException | |
org.springframework.http.HttpOutputMessage used
to write a MIME multipart.
| Modifier and Type | Field and Description |
|---|---|
| private final Charset | |
| private final HttpHeaders | |
| private boolean | |
| private final OutputStream |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public OutputStream | getBody()
Implements org. Return the body of the message as an output stream. |
| private byte[] | |
| public HttpHeaders | getHeaders()
Implements org. Return the headers of this message. |
| private void |
| charset | back to summary |
|---|---|
| private final Charset charset | |
| headers | back to summary |
|---|---|
| private final HttpHeaders headers | |
| headersWritten | back to summary |
|---|---|
| private boolean headersWritten | |
| outputStream | back to summary |
|---|---|
| private final OutputStream outputStream | |
| MultipartHttpOutputMessage | back to summary |
|---|---|
| public MultipartHttpOutputMessage(OutputStream outputStream, Charset charset) | |
| getBody | back to summary |
|---|---|
| public OutputStream getBody() throws IOException Implements org. Doc from org. Return the body of the message as an output stream.
| |
| getBytes | back to summary |
|---|---|
| private byte[] getBytes(String name) | |
| getHeaders | back to summary |
|---|---|
| public HttpHeaders getHeaders() Implements org. Doc from org. Return the headers of this message.
| |
| writeHeaders | back to summary |
|---|---|
| private void writeHeaders() throws IOException | |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Overrides java. Implements java. Closes this output stream and releases any system resources associated with the stream. |
| public void | flush()
Overrides java. Implements java. Flushes this output stream and forces any buffered output bytes to be written out to the stream. |
| public void | write(byte[]
the data. b, int the start offset in the data. off, int the number of bytes to write. let)Overrides java. Writes |
| MultipartOutputStream | back to summary |
|---|---|
| public MultipartOutputStream(OutputStream out) | |
| close | back to summary |
|---|---|
| public void close() Overrides java. Implements java. Doc from java. Closes this output stream and releases any system resources associated with the stream.
| |
| flush | back to summary |
|---|---|
| public void flush() Overrides java. Implements java. Doc from java. Flushes this output stream and forces any buffered output bytes to be written out to the stream.
| |
| write | back to summary |
|---|---|
| public void write(byte[] b, int off, int let) throws IOException Overrides java. Doc from java. Writes
| |