diff --git a/spring-web/src/main/java/org/springframework/web/multipart/MaxUploadSizeExceededException.java b/spring-web/src/main/java/org/springframework/web/multipart/MaxUploadSizeExceededException.java index fce24534467..b267f3b68eb 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/MaxUploadSizeExceededException.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/MaxUploadSizeExceededException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,7 +31,8 @@ public class MaxUploadSizeExceededException extends MultipartException { /** * Constructor for MaxUploadSizeExceededException. - * @param maxUploadSize the maximum upload size allowed + * @param maxUploadSize the maximum upload size allowed, + * or -1 if the size limit isn't known */ public MaxUploadSizeExceededException(long maxUploadSize) { this(maxUploadSize, null); @@ -39,17 +40,19 @@ public class MaxUploadSizeExceededException extends MultipartException { /** * Constructor for MaxUploadSizeExceededException. - * @param maxUploadSize the maximum upload size allowed + * @param maxUploadSize the maximum upload size allowed, + * or -1 if the size limit isn't known * @param ex root cause from multipart parsing API in use */ public MaxUploadSizeExceededException(long maxUploadSize, Throwable ex) { - super("Maximum upload size of " + maxUploadSize + " bytes exceeded", ex); + super("Maximum upload size " + (maxUploadSize >= 0 ? "of " + maxUploadSize + " bytes " : "") + "exceeded", ex); this.maxUploadSize = maxUploadSize; } /** - * Return the maximum upload size allowed. + * Return the maximum upload size allowed, + * or -1 if the size limit isn't known. */ public long getMaxUploadSize() { return this.maxUploadSize; diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java b/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java index b7fc59690ca..e696d11788b 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,6 +37,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.util.FileCopyUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.multipart.MultipartException; import org.springframework.web.multipart.MultipartFile; @@ -106,13 +107,17 @@ public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpSe } setMultipartFiles(files); } - catch (Exception ex) { - throw new MultipartException("Could not parse multipart servlet request", ex); + catch (Throwable ex) { + handleParseFailure(ex); } } - private String extractFilename(String contentDisposition) { - return extractFilename(contentDisposition, FILENAME_KEY); + protected void handleParseFailure(Throwable ex) { + String msg = ex.getMessage(); + if (msg != null && msg.contains("size") && msg.contains("exceed")) { + throw new MaxUploadSizeExceededException(-1, ex); + } + throw new MultipartException("Failed to parse multipart servlet request", ex); } private String extractFilename(String contentDisposition, String key) { @@ -139,6 +144,10 @@ public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpSe return filename; } + private String extractFilename(String contentDisposition) { + return extractFilename(contentDisposition, FILENAME_KEY); + } + private String extractFilenameWithCharset(String contentDisposition) { String filename = extractFilename(contentDisposition, FILENAME_WITH_CHARSET_KEY); if (filename == null) { @@ -219,7 +228,7 @@ public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpSe Part part = getPart(paramOrFileName); return (part != null ? part.getContentType() : null); } - catch (Exception ex) { + catch (Throwable ex) { throw new MultipartException("Could not access multipart servlet request", ex); } } @@ -239,7 +248,7 @@ public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpSe return null; } } - catch (Exception ex) { + catch (Throwable ex) { throw new MultipartException("Could not access multipart servlet request", ex); } }