org.springframework.web.multipart.support.MultipartFilterCustom Maven / Gradle / Ivy
package org.springframework.web.multipart.support;
import java.io.IOException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import javax.servlet.FilterChain;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartResolver;
/**
* @see org.springframework.session.web.http.SessionRepositoryFilter#DEFAULT_ORDER
* @see org.springframework.boot.autoconfigure.security.SecurityProperties#DEFAULT_FILTER_ORDER
* @see org.springframework.web.servlet.DispatcherServlet#MULTIPART_RESOLVER_BEAN_NAME
*/
public class MultipartFilterCustom extends MultipartFilter {
private static final BigInteger ONE_KB_BI = BigInteger.valueOf(1024);
private static final BigInteger ONE_MB_BI = ONE_KB_BI.multiply(ONE_KB_BI);
private static final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI);
private static final BigInteger ONE_TB_BI = ONE_KB_BI.multiply(ONE_GB_BI);
private static final BigInteger ONE_PB_BI = ONE_KB_BI.multiply(ONE_TB_BI);
private static final BigInteger ONE_EB_BI = ONE_KB_BI.multiply(ONE_PB_BI);
public static final String DEFAULT_MULTIPART_CONFIG_ELEMENT_BEAN_NAME = "multipartConfigElement";
private final MultipartConfigElement defaultMultipartConfigElement = new MultipartConfigElement((String) null);
private String multipartConfigElementBeanName = DEFAULT_MULTIPART_CONFIG_ELEMENT_BEAN_NAME;
private MultipartResolver multipartResolver;
public MultipartFilterCustom(MultipartResolver multipartResolver) {
this.multipartResolver = multipartResolver;
}
/**
* @see org.springframework.web.multipart.MaxUploadSizeExceededException
*/
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
try {
super.doFilterInternal(request, response, filterChain);
}
catch (Exception e) {
if (e.getCause() instanceof MultipartException) {
Throwable throwable = ((MultipartException) e.getCause()).getCause();
if (throwable instanceof IllegalStateException && (throwable = throwable.getCause()) instanceof Throwable) {
Method method = ReflectionUtils.findMethod(throwable.getClass(), "getPermittedSize");
if (method != null) {
Object object = ReflectionUtils.invokeMethod(method, throwable);
if (object instanceof Long) {
throw new MaxUploadSizeExceededException((Long) object, throwable);
}
}
}
}
if (e.getCause() instanceof IllegalStateException) {
Throwable throwable = ((IllegalStateException) e.getCause()).getCause();
if (throwable instanceof Throwable) {
Method method = ReflectionUtils.findMethod(throwable.getClass(), "getPermittedSize");
if (method != null) {
Object object = ReflectionUtils.invokeMethod(method, throwable);
if (object instanceof Long) {
throw new MaxUploadSizeExceededException((Long) object, throwable);
}
}
}
}
throw e;
// throw new MultipartException("파일 크기가 초과되었습니다. (최대 " +
// byteCountToDisplaySize(BigInteger.valueOf(lookupMultipartConfigElement(request).getMaxFileSize()))
// + " 제한)", e);
}
}
@Override
protected MultipartResolver lookupMultipartResolver() {
MultipartResolver multipartResolver = this.multipartResolver;
if (multipartResolver == null) {
multipartResolver = super.lookupMultipartResolver();
}
return multipartResolver;
}
protected MultipartConfigElement lookupMultipartConfigElement(HttpServletRequest request) {
return lookupMultipartConfigElement();
}
protected MultipartConfigElement lookupMultipartConfigElement() {
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
String beanName = getMultipartConfigElementBeanName();
if (wac != null && wac.containsBean(beanName)) {
if (logger.isDebugEnabled()) {
logger.debug("Using MultipartConfigElement '" + beanName + "' for MultipartFilter");
}
return wac.getBean(beanName, MultipartConfigElement.class);
}
else {
return this.defaultMultipartConfigElement;
}
}
/**
* @see org.apache.commons.io.FileUtils#byteCountToDisplaySize(BigInteger)
*/
public static String byteCountToDisplaySize(BigInteger size) {
String displaySize;
if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_EB_BI)) + " EB";
}
else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_PB_BI)) + " PB";
}
else if (size.divide(ONE_TB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_TB_BI)) + " TB";
}
else if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_GB_BI)) + " GB";
}
else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_MB_BI)) + " MB";
}
else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_KB_BI)) + " KB";
}
else {
displaySize = String.valueOf(size) + " bytes";
}
return displaySize;
}
public String getMultipartConfigElementBeanName() {
return multipartConfigElementBeanName;
}
public void setMultipartConfigElementBeanName(String multipartConfigElementBeanName) {
this.multipartConfigElementBeanName = multipartConfigElementBeanName;
}
}