MultipartResolver.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 5k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.web.multipart;
  17. import javax.servlet.http.HttpServletRequest;
  18. /**
  19.  * Interface for multipart resolution strategies that handle file uploads as
  20.  * defined in <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>.
  21.  * Implementations are typically usable both within any application context
  22.  * and standalone.
  23.  *
  24.  * <p>There are two concrete implementations included in Spring:
  25.  * <ul>
  26.  * <li>CommonsMultipartResolver for Jakarta Commons FileUpload
  27.  * <li>CosMultipartResolver for Jason Hunter's COS (com.oreilly.servlet)
  28.  * </ul>
  29.  *
  30.  * <p>There is no default resolver implementation used for Spring DispatcherServlets,
  31.  * as an application might choose to parse its multipart requests itself. To define
  32.  * an implementation, create a bean with the id "multipartResolver" in a
  33.  * DispatcherServlet's application context. Such a resolver gets applied to all
  34.  * requests handled by that DispatcherServlet.
  35.  *
  36.  * <p>If a DispatcherServlet detects a multipart request, it will resolve it
  37.  * via the configured MultipartResolver and pass on a wrapped HttpServletRequest.
  38.  * Controllers can then cast their given request to the MultipartHttpServletRequest
  39.  * interface, being able to access MultipartFiles. Note that this cast is only
  40.  * supported in case of an actual multipart request.
  41.  *
  42.  * <pre>
  43.  * ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
  44.  *   MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  45.  *   MultipartFile multipartFile = multipartRequest.getFile("image");
  46.  *   ...
  47.  * }</pre>
  48.  *
  49.  * <p>Instead of direct access, command respectively form controllers can register
  50.  * a ByteArrayMultipartFileEditor or StringMultipartFileEditor with their data
  51.  * binder, to automatically apply multipart content to command bean properties.
  52.  *
  53.  * <p>As an alternative to using a MultipartResolver with a DispatcherServlet,
  54.  * a MultipartFilter can be registered in web.xml. It will delegate to a
  55.  * corresponding MultipartResolver bean in the root application context.
  56.  * This is mainly intended for applications that do not use Spring's own
  57.  * web MVC framework.
  58.  *
  59.  * <p>Note: There is hardly ever a need to access the MultipartResolver itself
  60.  * from application code. It will simply do its work behind the scenes,
  61.  * making MultipartHttpServletRequests available to controllers.
  62.  *
  63.  * @author Juergen Hoeller
  64.  * @author Trevor D. Cook
  65.  * @since 29.9.2003
  66.  * @see MultipartHttpServletRequest
  67.  * @see MultipartFile
  68.  * @see org.springframework.web.multipart.commons.CommonsMultipartResolver
  69.  * @see org.springframework.web.multipart.cos.CosMultipartResolver
  70.  * @see org.springframework.web.multipart.support.ByteArrayMultipartFileEditor
  71.  * @see org.springframework.web.multipart.support.StringMultipartFileEditor
  72.  * @see org.springframework.web.servlet.DispatcherServlet
  73.  * @see org.springframework.web.servlet.support.RequestContextUtils#getMultipartResolver
  74.  */
  75. public interface MultipartResolver {
  76. /**
  77.  * Determine if the request contains multipart content.
  78.  * <p>Will typically check for content type "multipart/form-data", but the actually
  79.  * accepted requests might depend on the capabilities of the resolver implementation.
  80.  * @param request the servlet request to be evaluated
  81.  * @return whether the request contains multipart content
  82.  */
  83. boolean isMultipart(HttpServletRequest request);
  84. /**
  85.  * Parse the given HTTP request into multipart files and parameters,
  86.  * and wrap the request inside a MultipartHttpServletRequest object
  87.  * that provides access to file descriptors and makes contained
  88.  * parameters accessible via the standard ServletRequest methods.
  89.  * @param request the servlet request to wrap (must be of a multipart content type)
  90.  * @return the wrapped servlet request
  91.  * @throws MultipartException if the servlet request is not multipart, or if
  92.  * implementation-specific problems are encountered (such as exceeding file size limits)
  93.  * @see MultipartHttpServletRequest#getFile
  94.  * @see MultipartHttpServletRequest#getFileNames
  95.  * @see MultipartHttpServletRequest#getFileMap
  96.  * @see javax.servlet.http.HttpServletRequest#getParameter
  97.  * @see javax.servlet.http.HttpServletRequest#getParameterNames
  98.  * @see javax.servlet.http.HttpServletRequest#getParameterMap
  99.  */
  100. MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException;
  101. /**
  102.  * Cleanup any resources used for the multipart handling,
  103.  * like a storage for the uploaded files.
  104.  * @param request the request to cleanup resources for
  105.  */
  106. void cleanupMultipart(MultipartHttpServletRequest request);
  107. }