001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.multipart.support; 018 019import javax.servlet.http.HttpServletRequest; 020import javax.servlet.http.Part; 021 022import org.apache.commons.logging.LogFactory; 023 024import org.springframework.util.StringUtils; 025import org.springframework.web.multipart.MultipartException; 026import org.springframework.web.multipart.MultipartHttpServletRequest; 027import org.springframework.web.multipart.MultipartResolver; 028 029/** 030 * Standard implementation of the {@link MultipartResolver} interface, 031 * based on the Servlet 3.0 {@link javax.servlet.http.Part} API. 032 * To be added as "multipartResolver" bean to a Spring DispatcherServlet context, 033 * without any extra configuration at the bean level (see below). 034 * 035 * <p><b>Note:</b> In order to use Servlet 3.0 based multipart parsing, 036 * you need to mark the affected servlet with a "multipart-config" section in 037 * {@code web.xml}, or with a {@link javax.servlet.MultipartConfigElement} 038 * in programmatic servlet registration, or (in case of a custom servlet class) 039 * possibly with a {@link javax.servlet.annotation.MultipartConfig} annotation 040 * on your servlet class. Configuration settings such as maximum sizes or 041 * storage locations need to be applied at that servlet registration level; 042 * Servlet 3.0 does not allow for them to be set at the MultipartResolver level. 043 * 044 * <pre class="code"> 045 * public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 046 * // ... 047 * @Override 048 * protected void customizeRegistration(ServletRegistration.Dynamic registration) { 049 * // Optionally also set maxFileSize, maxRequestSize, fileSizeThreshold 050 * registration.setMultipartConfig(new MultipartConfigElement("/tmp")); 051 * } 052 * } 053 * </pre> 054 * 055 * @author Juergen Hoeller 056 * @since 3.1 057 * @see #setResolveLazily 058 * @see HttpServletRequest#getParts() 059 * @see org.springframework.web.multipart.commons.CommonsMultipartResolver 060 */ 061public class StandardServletMultipartResolver implements MultipartResolver { 062 063 private boolean resolveLazily = false; 064 065 066 /** 067 * Set whether to resolve the multipart request lazily at the time of 068 * file or parameter access. 069 * <p>Default is "false", resolving the multipart elements immediately, throwing 070 * corresponding exceptions at the time of the {@link #resolveMultipart} call. 071 * Switch this to "true" for lazy multipart parsing, throwing parse exceptions 072 * once the application attempts to obtain multipart files or parameters. 073 * @since 3.2.9 074 */ 075 public void setResolveLazily(boolean resolveLazily) { 076 this.resolveLazily = resolveLazily; 077 } 078 079 080 @Override 081 public boolean isMultipart(HttpServletRequest request) { 082 return StringUtils.startsWithIgnoreCase(request.getContentType(), "multipart/"); 083 } 084 085 @Override 086 public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException { 087 return new StandardMultipartHttpServletRequest(request, this.resolveLazily); 088 } 089 090 @Override 091 public void cleanupMultipart(MultipartHttpServletRequest request) { 092 if (!(request instanceof AbstractMultipartHttpServletRequest) || 093 ((AbstractMultipartHttpServletRequest) request).isResolved()) { 094 // To be on the safe side: explicitly delete the parts, 095 // but only actual file parts (for Resin compatibility) 096 try { 097 for (Part part : request.getParts()) { 098 if (request.getFile(part.getName()) != null) { 099 part.delete(); 100 } 101 } 102 } 103 catch (Throwable ex) { 104 LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex); 105 } 106 } 107 } 108 109}