Skip to content

Commit 1922f03

Browse files
committed
StandardMultipartHttpServletRequest identifies MaxUploadSizeExceededException through keywords in message
Issue: SPR-9294
1 parent 62e530e commit 1922f03

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

spring-web/src/main/java/org/springframework/web/multipart/MaxUploadSizeExceededException.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,25 +31,28 @@ public class MaxUploadSizeExceededException extends MultipartException {
3131

3232
/**
3333
* Constructor for MaxUploadSizeExceededException.
34-
* @param maxUploadSize the maximum upload size allowed
34+
* @param maxUploadSize the maximum upload size allowed,
35+
* or -1 if the size limit isn't known
3536
*/
3637
public MaxUploadSizeExceededException(long maxUploadSize) {
3738
this(maxUploadSize, null);
3839
}
3940

4041
/**
4142
* Constructor for MaxUploadSizeExceededException.
42-
* @param maxUploadSize the maximum upload size allowed
43+
* @param maxUploadSize the maximum upload size allowed,
44+
* or -1 if the size limit isn't known
4345
* @param ex root cause from multipart parsing API in use
4446
*/
4547
public MaxUploadSizeExceededException(long maxUploadSize, Throwable ex) {
46-
super("Maximum upload size of " + maxUploadSize + " bytes exceeded", ex);
48+
super("Maximum upload size " + (maxUploadSize >= 0 ? "of " + maxUploadSize + " bytes " : "") + "exceeded", ex);
4749
this.maxUploadSize = maxUploadSize;
4850
}
4951

5052

5153
/**
52-
* Return the maximum upload size allowed.
54+
* Return the maximum upload size allowed,
55+
* or -1 if the size limit isn't known.
5356
*/
5457
public long getMaxUploadSize() {
5558
return this.maxUploadSize;

spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737
import org.springframework.util.FileCopyUtils;
3838
import org.springframework.util.LinkedMultiValueMap;
3939
import org.springframework.util.MultiValueMap;
40+
import org.springframework.web.multipart.MaxUploadSizeExceededException;
4041
import org.springframework.web.multipart.MultipartException;
4142
import org.springframework.web.multipart.MultipartFile;
4243

@@ -106,13 +107,17 @@ private void parseRequest(HttpServletRequest request) {
106107
}
107108
setMultipartFiles(files);
108109
}
109-
catch (Exception ex) {
110-
throw new MultipartException("Could not parse multipart servlet request", ex);
110+
catch (Throwable ex) {
111+
handleParseFailure(ex);
111112
}
112113
}
113114

114-
private String extractFilename(String contentDisposition) {
115-
return extractFilename(contentDisposition, FILENAME_KEY);
115+
protected void handleParseFailure(Throwable ex) {
116+
String msg = ex.getMessage();
117+
if (msg != null && msg.contains("size") && msg.contains("exceed")) {
118+
throw new MaxUploadSizeExceededException(-1, ex);
119+
}
120+
throw new MultipartException("Failed to parse multipart servlet request", ex);
116121
}
117122

118123
private String extractFilename(String contentDisposition, String key) {
@@ -139,6 +144,10 @@ private String extractFilename(String contentDisposition, String key) {
139144
return filename;
140145
}
141146

147+
private String extractFilename(String contentDisposition) {
148+
return extractFilename(contentDisposition, FILENAME_KEY);
149+
}
150+
142151
private String extractFilenameWithCharset(String contentDisposition) {
143152
String filename = extractFilename(contentDisposition, FILENAME_WITH_CHARSET_KEY);
144153
if (filename == null) {
@@ -219,7 +228,7 @@ public String getMultipartContentType(String paramOrFileName) {
219228
Part part = getPart(paramOrFileName);
220229
return (part != null ? part.getContentType() : null);
221230
}
222-
catch (Exception ex) {
231+
catch (Throwable ex) {
223232
throw new MultipartException("Could not access multipart servlet request", ex);
224233
}
225234
}
@@ -239,7 +248,7 @@ public HttpHeaders getMultipartHeaders(String paramOrFileName) {
239248
return null;
240249
}
241250
}
242-
catch (Exception ex) {
251+
catch (Throwable ex) {
243252
throw new MultipartException("Could not access multipart servlet request", ex);
244253
}
245254
}

0 commit comments

Comments
 (0)