org.springframework.web.multipart.commons
Class CommonsMultipartResolver

java.lang.Object
  extended byorg.springframework.web.multipart.commons.CommonsMultipartResolver
All Implemented Interfaces:
MultipartResolver, ServletContextAware

public class CommonsMultipartResolver
extends java.lang.Object
implements MultipartResolver, ServletContextAware

MultipartResolver implementation for Jakarta Commons FileUpload.

Provides maximumFileSize, maximumInMemorySize, and headerEncoding settings as bean properties; see respective DiskFileUpload properties (sizeMax, sizeThreshold, headerEncoding) for details in terms of defaults and accepted values.

Saves temporary files to the servlet container's temporary directory. Needs to be initialized either by an application context or via the constructor that takes a ServletContext (for standalone usage).

Since:
29-Sep-2003
Author:
Trevor D. Cook, Juergen Hoeller
See Also:
CommonsMultipartResolver(ServletContext), CommonsMultipartFile, DiskFileUpload

Field Summary
protected  org.apache.commons.logging.Log logger
           
 
Constructor Summary
CommonsMultipartResolver()
          Constructor for use as bean.
CommonsMultipartResolver(javax.servlet.ServletContext servletContext)
          Constructor for standalone usage.
 
Method Summary
 void cleanupMultipart(MultipartHttpServletRequest request)
          Cleanup any resources used for the multipart handling, like a storage for the uploaded files.
protected  java.lang.String determineEncoding(javax.servlet.http.HttpServletRequest request)
          Determine the encoding for the given request.
 org.apache.commons.fileupload.DiskFileUpload getFileUpload()
          Return the underlying org.apache.commons.fileupload.DiskFileUpload instance.
 boolean isMultipart(javax.servlet.http.HttpServletRequest request)
          Determine if the request contains multipart content.
protected  org.apache.commons.fileupload.DiskFileUpload newFileUpload()
          Initialize the underlying org.apache.commons.fileupload.DiskFileUpload instance.
 MultipartHttpServletRequest resolveMultipart(javax.servlet.http.HttpServletRequest request)
          Wrap the servlet request inside a MultipartHttpServletRequest.
 void setDefaultEncoding(java.lang.String defaultEncoding)
          Set the default character encoding to use for parsing requests, to be applied to headers of individual parts and to form fields.
 void setMaxInMemorySize(int maxInMemorySize)
          Set the maximum allowed size (in bytes) before uploads are written to disk.
 void setMaxUploadSize(long maxUploadSize)
          Set the maximum allowed size (in bytes) before uploads are refused.
 void setServletContext(javax.servlet.ServletContext servletContext)
          Set the ServletContext that this object runs in.
 void setUploadTempDir(Resource uploadTempDir)
          Set the temporary directory where uploaded files get stored.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.apache.commons.logging.Log logger
Constructor Detail

CommonsMultipartResolver

public CommonsMultipartResolver()
Constructor for use as bean. Determines the servlet container's temporary directory via the ServletContext passed in as through the ServletContextAware interface (typically by a WebApplicationContext).

See Also:
setServletContext(javax.servlet.ServletContext), ServletContextAware, WebApplicationContext

CommonsMultipartResolver

public CommonsMultipartResolver(javax.servlet.ServletContext servletContext)
Constructor for standalone usage. Determines the servlet container's temporary directory via the given ServletContext.

Parameters:
servletContext - the ServletContext to use
Method Detail

newFileUpload

protected org.apache.commons.fileupload.DiskFileUpload newFileUpload()
Initialize the underlying org.apache.commons.fileupload.DiskFileUpload instance. Can be overridden to use a custom subclass, e.g. for testing purposes.

Returns:
the new DiskFileUpload instance

getFileUpload

public org.apache.commons.fileupload.DiskFileUpload getFileUpload()
Return the underlying org.apache.commons.fileupload.DiskFileUpload instance. There is hardly any need to access this.

Returns:
the underlying DiskFileUpload instance

setMaxUploadSize

public void setMaxUploadSize(long maxUploadSize)
Set the maximum allowed size (in bytes) before uploads are refused. -1 indicates no limit (the default).

Parameters:
maxUploadSize - the maximum upload size allowed
See Also:
FileUploadBase.setSizeMax(long)

setMaxInMemorySize

public void setMaxInMemorySize(int maxInMemorySize)
Set the maximum allowed size (in bytes) before uploads are written to disk. Uploaded files will still be received past this amount, but they will not be stored in memory. Default is 10240, according to Commons FileUpload.

Parameters:
maxInMemorySize - the maximum in memory size allowed
See Also:
DiskFileUpload.setSizeThreshold(int)

setDefaultEncoding

public void setDefaultEncoding(java.lang.String defaultEncoding)
Set the default character encoding to use for parsing requests, to be applied to headers of individual parts and to form fields. Default is ISO-8859-1, according to the Servlet spec.

If the request specifies a character encoding itself, the request encoding will override this setting. This also allows for generically overriding the character encoding in a filter that invokes the ServletRequest.setCharacterEncoding method.

Parameters:
defaultEncoding - the character encoding to use
See Also:
determineEncoding(javax.servlet.http.HttpServletRequest), ServletRequest.getCharacterEncoding(), ServletRequest.setCharacterEncoding(java.lang.String), WebUtils.DEFAULT_CHARACTER_ENCODING, FileUploadBase.setHeaderEncoding(java.lang.String)

setUploadTempDir

public void setUploadTempDir(Resource uploadTempDir)
                      throws java.io.IOException
Set the temporary directory where uploaded files get stored. Default is the servlet container's temporary directory for the web application.

Throws:
java.io.IOException
See Also:
WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE

setServletContext

public void setServletContext(javax.servlet.ServletContext servletContext)
Description copied from interface: ServletContextAware
Set the ServletContext that this object runs in.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked after ApplicationContextAware's setApplicationContext.

Specified by:
setServletContext in interface ServletContextAware
Parameters:
servletContext - ServletContext object to be used by this object

isMultipart

public boolean isMultipart(javax.servlet.http.HttpServletRequest request)
Description copied from interface: MultipartResolver
Determine if the request contains multipart content.

Will typically check for content type "multipart/form-data", but the actually accepted requests might depend on the capabilities of the resolver implementation.

Specified by:
isMultipart in interface MultipartResolver
Parameters:
request - the servlet request to be evaluated
Returns:
true if the request contains multipart content; false otherwise

resolveMultipart

public MultipartHttpServletRequest resolveMultipart(javax.servlet.http.HttpServletRequest request)
                                             throws MultipartException
Description copied from interface: MultipartResolver
Wrap the servlet request inside a MultipartHttpServletRequest.

Specified by:
resolveMultipart in interface MultipartResolver
Parameters:
request - the servlet request to wrap (must be of a multipart content type)
Returns:
the wrapped servlet request
Throws:
MultipartException - if the servlet request is not multipart, or if implementation-specific problems are encountered (such as exceeding file size limits)

determineEncoding

protected java.lang.String determineEncoding(javax.servlet.http.HttpServletRequest request)
Determine the encoding for the given request. Can be overridden in subclasses.

The default implementation checks the request encoding, falling back to the default encoding specified for this resolver.

Parameters:
request - current HTTP request
Returns:
the encoding for the request (never null)
See Also:
ServletRequest.getCharacterEncoding(), setDefaultEncoding(java.lang.String)

cleanupMultipart

public void cleanupMultipart(MultipartHttpServletRequest request)
Description copied from interface: MultipartResolver
Cleanup any resources used for the multipart handling, like a storage for the uploaded files.

Specified by:
cleanupMultipart in interface MultipartResolver
Parameters:
request - the request to cleanup resources for


Copyright (C) 2003-2004 The Spring Framework Project.