StandardMultipartHttpServletRequest identifies MaxUploadSizeExceededException through keywords in message

Issue: SPR-9294
This commit is contained in:
Juergen Hoeller 2017-01-16 21:45:22 +01:00
parent 62e530ec94
commit 1922f03d24
2 changed files with 24 additions and 12 deletions

View File

@ -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;

View File

@ -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);
}
}