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 * @author Juergen Hoeller 045 * @since 3.1 046 * @see #setResolveLazily 047 * @see HttpServletRequest#getParts() 048 * @see org.springframework.web.multipart.commons.CommonsMultipartResolver 049 */ 050public class StandardServletMultipartResolver implements MultipartResolver { 051 052 private boolean resolveLazily = false; 053 054 055 /** 056 * Set whether to resolve the multipart request lazily at the time of 057 * file or parameter access. 058 * <p>Default is "false", resolving the multipart elements immediately, throwing 059 * corresponding exceptions at the time of the {@link #resolveMultipart} call. 060 * Switch this to "true" for lazy multipart parsing, throwing parse exceptions 061 * once the application attempts to obtain multipart files or parameters. 062 * @since 3.2.9 063 */ 064 public void setResolveLazily(boolean resolveLazily) { 065 this.resolveLazily = resolveLazily; 066 } 067 068 069 @Override 070 public boolean isMultipart(HttpServletRequest request) { 071 // Same check as in Commons FileUpload... 072 if (!"post".equalsIgnoreCase(request.getMethod())) { 073 return false; 074 } 075 String contentType = request.getContentType(); 076 return StringUtils.startsWithIgnoreCase(contentType, "multipart/"); 077 } 078 079 @Override 080 public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException { 081 return new StandardMultipartHttpServletRequest(request, this.resolveLazily); 082 } 083 084 @Override 085 public void cleanupMultipart(MultipartHttpServletRequest request) { 086 if (!(request instanceof AbstractMultipartHttpServletRequest) || 087 ((AbstractMultipartHttpServletRequest) request).isResolved()) { 088 // To be on the safe side: explicitly delete the parts, 089 // but only actual file parts (for Resin compatibility) 090 try { 091 for (Part part : request.getParts()) { 092 if (request.getFile(part.getName()) != null) { 093 part.delete(); 094 } 095 } 096 } 097 catch (Throwable ex) { 098 LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex); 099 } 100 } 101 } 102 103}