Skip to content

Commit 3009e29

Browse files
committed
Remove contentDispositionFormData with charset method
The method was orginally added under SPR-14547 but the example in it was probably intended for use with Content-Disposition server response header (file dowonload) and not for a Content-Disposition header within the body of a multipart request. In a Spring application a multipart request is typically serialized by the FormHttpMessageConverter and hence the Content-Disposition is not explicitly set by the application. Issue: SPR-15205
1 parent d8a80fc commit 3009e29

File tree

2 files changed

+25
-37
lines changed

2 files changed

+25
-37
lines changed

spring-web/src/main/java/org/springframework/http/ContentDisposition.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
import org.springframework.util.ObjectUtils;
2828
import org.springframework.util.StringUtils;
2929

30-
import static java.nio.charset.StandardCharsets.*;
31-
import static java.time.format.DateTimeFormatter.*;
30+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
31+
import static java.nio.charset.StandardCharsets.UTF_8;
32+
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
3233

3334
/**
3435
* Represent the Content-Disposition type and parameters as defined in RFC 2183.
@@ -416,8 +417,14 @@ public interface Builder {
416417
Builder filename(String filename);
417418

418419
/**
419-
* Set the value of the {@literal filename*} that will be encoded as defined in
420-
* the RFC 5987. Only the US-ASCII, UTF-8 and ISO-8859-1 charsets are supported.
420+
* Set the value of the {@literal filename*} that will be encoded as
421+
* defined in the RFC 5987. Only the US-ASCII, UTF-8 and ISO-8859-1
422+
* charsets are supported.
423+
* <p><strong>Note:</strong> Do not use this for a
424+
* {@code "multipart/form-data"} requests as per
425+
* <a link="https://tools.ietf.org/html/rfc7578#section-4.2">RFC 7578, Section 4.2</a>
426+
* and also RFC 5987 itself mentions it does not apply to multipart
427+
* requests.
421428
*/
422429
Builder filename(String filename, Charset charset);
423430

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -742,48 +742,32 @@ public List<String> getConnection() {
742742
}
743743

744744
/**
745-
* Set the (new) value of the {@code Content-Disposition} header
746-
* for {@code form-data}.
745+
* Set the {@code Content-Disposition} header when creating a
746+
* {@code "multipart/form-data"} request.
747+
* <p>Applications typically would not set this header directly but
748+
* rather prepare a {@code MultiValueMap<String, Object>}, containing an
749+
* Object or a {@link org.springframework.core.io.Resource} for each part,
750+
* and then pass that to the {@code RestTemplate} or {@code WebClient}.
747751
* @param name the control name
748752
* @param filename the filename (may be {@code null})
749-
* @see #setContentDisposition(ContentDisposition)
750753
* @see #getContentDisposition()
751754
*/
752755
public void setContentDispositionFormData(String name, @Nullable String filename) {
753-
setContentDispositionFormData(name, filename, null);
754-
}
755-
756-
/**
757-
* Set the (new) value of the {@code Content-Disposition} header
758-
* for {@code form-data}, optionally encoding the filename using the RFC 5987.
759-
* <p>Only the US-ASCII, UTF-8 and ISO-8859-1 charsets are supported.
760-
* @param name the control name
761-
* @param filename the filename (may be {@code null})
762-
* @param charset the charset used for the filename (may be {@code null})
763-
* @since 4.3.3
764-
* @see #setContentDisposition(ContentDisposition)
765-
* @see #getContentDisposition()
766-
* @see <a href="https://tools.ietf.org/html/rfc7230#section-3.2.4">RFC 7230 Section 3.2.4</a>
767-
*/
768-
public void setContentDispositionFormData(String name, @Nullable String filename, @Nullable Charset charset) {
769756
Assert.notNull(name, "'name' must not be null");
770757
ContentDisposition.Builder disposition = ContentDisposition.builder("form-data").name(name);
771758
if (filename != null) {
772-
if (charset != null) {
773-
disposition.filename(filename, charset);
774-
}
775-
else {
776-
disposition.filename(filename);
777-
}
759+
disposition.filename(filename);
778760
}
779761
setContentDisposition(disposition.build());
780762
}
781763

782764
/**
783-
* Set the (new) value of the {@literal Content-Disposition} header. Supports the
784-
* disposition type and {@literal filename}, {@literal filename*} (encoded according
785-
* to RFC 5987, only the US-ASCII, UTF-8 and ISO-8859-1 charsets are supported),
786-
* {@literal name}, {@literal size} parameters.
765+
* Set the {@literal Content-Disposition} header.
766+
* <p>This could be used on a response to indicate if the content is
767+
* expected to be displayed inline in the browser or as an attachment to be
768+
* saved locally.
769+
* <p>It can also be used for a {@code "multipart/form-data"} request.
770+
* For more details see notes on {@link #setContentDispositionFormData}.
787771
* @since 5.0
788772
* @see #getContentDisposition()
789773
*/
@@ -792,10 +776,7 @@ public void setContentDisposition(ContentDisposition contentDisposition) {
792776
}
793777

794778
/**
795-
* Return the {@literal Content-Disposition} header parsed as a {@link ContentDisposition}
796-
* instance. Supports the disposition type and {@literal filename}, {@literal filename*}
797-
* (encoded according to RFC 5987, only the US-ASCII, UTF-8 and ISO-8859-1 charsets are
798-
* supported), {@literal name}, {@literal size} parameters.
779+
* Return a parsed representation of the {@literal Content-Disposition} header.
799780
* @since 5.0
800781
* @see #setContentDisposition(ContentDisposition)
801782
*/

0 commit comments

Comments
 (0)