diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index a658f08e9e..7ea19ab8d6 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -670,13 +670,24 @@ public class HttpHeaders implements MultiValueMap, Serializable } /** - * Set the (new) value of the {@code Content-Disposition} header - * for {@code form-data}. + * Set the {@code Content-Disposition} header when creating a + * {@code "multipart/form-data"} request. + *

Applications typically would not set this header directly but + * rather prepare a {@code MultiValueMap}, containing an + * Object or a {@link org.springframework.core.io.Resource} for each part, + * and then pass that to the {@code RestTemplate} or {@code WebClient}. * @param name the control name * @param filename the filename (may be {@code null}) */ public void setContentDispositionFormData(String name, String filename) { - setContentDispositionFormData(name, filename, null); + Assert.notNull(name, "'name' must not be null"); + StringBuilder builder = new StringBuilder("form-data; name=\""); + builder.append(name).append('\"'); + if (filename != null) { + builder.append("; filename=\""); + builder.append(filename).append('\"'); + } + set(CONTENT_DISPOSITION, builder.toString()); } /** @@ -686,10 +697,14 @@ public class HttpHeaders implements MultiValueMap, Serializable * @param name the control name * @param filename the filename (may be {@code null}) * @param charset the charset used for the filename (may be {@code null}) - * @since 4.3.3 - * @see #setContentDispositionFormData(String, String) - * @see RFC 7230 Section 3.2.4 + * @deprecated deprecated in 4.3.11 and removed from 5.0; as per + * RFC 7578, Section 4.2, + * an RFC 5987 style encoding should not be used for multipart/form-data requests. + * Furthermore there should be no reason for applications to set this header + * explicitly; for more details also read + * {@link #setContentDispositionFormData(String, String)} */ + @Deprecated public void setContentDispositionFormData(String name, String filename, Charset charset) { Assert.notNull(name, "'name' must not be null"); StringBuilder builder = new StringBuilder("form-data; name=\""); diff --git a/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java b/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java index c8098be86b..06c0e15e42 100644 --- a/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java +++ b/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java @@ -311,6 +311,7 @@ public class HttpHeadersTests { assertThat(headers.getCacheControl(), is("max-age=1000, public, s-maxage=1000")); } + @SuppressWarnings("deprecation") @Test public void contentDisposition() { headers.setContentDispositionFormData("name", null);