StandardServletMultipartResolver.java 4.1 KB
Newer Older
C
Chris Beams 已提交
1
/*
2
 * Copyright 2002-2018 the original author or authors.
C
Chris Beams 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.multipart.support;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

import org.apache.commons.logging.LogFactory;

24
import org.springframework.util.StringUtils;
C
Chris Beams 已提交
25 26 27 28 29 30 31 32 33 34 35 36
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;

/**
 * Standard implementation of the {@link MultipartResolver} interface,
 * based on the Servlet 3.0 {@link javax.servlet.http.Part} API.
 * To be added as "multipartResolver" bean to a Spring DispatcherServlet context,
 * without any extra configuration at the bean level (see below).
 *
 * <p><b>Note:</b> In order to use Servlet 3.0 based multipart parsing,
 * you need to mark the affected servlet with a "multipart-config" section in
37
 * {@code web.xml}, or with a {@link javax.servlet.MultipartConfigElement}
C
Chris Beams 已提交
38 39 40 41 42 43
 * in programmatic servlet registration, or (in case of a custom servlet class)
 * possibly with a {@link javax.servlet.annotation.MultipartConfig} annotation
 * on your servlet class. Configuration settings such as maximum sizes or
 * storage locations need to be applied at that servlet registration level;
 * Servlet 3.0 does not allow for them to be set at the MultipartResolver level.
 *
44 45 46 47 48 49 50 51 52 53 54 55
 * <pre class="code">
 * public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
 *	// ...
 *	&#064;Override
 *	protected void customizeRegistration(ServletRegistration.Dynamic registration) {
 *
 *		// Optionally also set maxFileSize, maxRequestSize, fileSizeThreshold
 *		registration.setMultipartConfig(new MultipartConfigElement("/tmp"));
 *	}
 * }
 * </pre>
 *
C
Chris Beams 已提交
56 57
 * @author Juergen Hoeller
 * @since 3.1
J
Juergen Hoeller 已提交
58 59 60
 * @see #setResolveLazily
 * @see HttpServletRequest#getParts()
 * @see org.springframework.web.multipart.commons.CommonsMultipartResolver
C
Chris Beams 已提交
61 62 63
 */
public class StandardServletMultipartResolver implements MultipartResolver {

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	private boolean resolveLazily = false;


	/**
	 * Set whether to resolve the multipart request lazily at the time of
	 * file or parameter access.
	 * <p>Default is "false", resolving the multipart elements immediately, throwing
	 * corresponding exceptions at the time of the {@link #resolveMultipart} call.
	 * Switch this to "true" for lazy multipart parsing, throwing parse exceptions
	 * once the application attempts to obtain multipart files or parameters.
	 */
	public void setResolveLazily(boolean resolveLazily) {
		this.resolveLazily = resolveLazily;
	}


80
	@Override
C
Chris Beams 已提交
81 82
	public boolean isMultipart(HttpServletRequest request) {
		// Same check as in Commons FileUpload...
83
		if (!"post".equalsIgnoreCase(request.getMethod())) {
C
Chris Beams 已提交
84 85 86
			return false;
		}
		String contentType = request.getContentType();
87
		return StringUtils.startsWithIgnoreCase(contentType, "multipart/");
C
Chris Beams 已提交
88 89
	}

90
	@Override
C
Chris Beams 已提交
91
	public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException {
92
		return new StandardMultipartHttpServletRequest(request, this.resolveLazily);
C
Chris Beams 已提交
93 94
	}

95
	@Override
C
Chris Beams 已提交
96
	public void cleanupMultipart(MultipartHttpServletRequest request) {
97 98
		// To be on the safe side: explicitly delete the parts,
		// but only actual file parts (for Resin compatibility)
C
Chris Beams 已提交
99 100
		try {
			for (Part part : request.getParts()) {
101 102 103
				if (request.getFile(part.getName()) != null) {
					part.delete();
				}
C
Chris Beams 已提交
104 105
			}
		}
J
Juergen Hoeller 已提交
106
		catch (Throwable ex) {
C
Chris Beams 已提交
107 108 109 110 111
			LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex);
		}
	}

}