Class CommonsFileUploadSupport

java.lang.Object
org.springframework.web.multipart.commons.CommonsFileUploadSupport
Direct Known Subclasses:
CommonsMultipartResolver

public abstract class CommonsFileUploadSupport extends Object
Base class for multipart resolvers that use Apache Commons FileUpload 1.2 or above.

Provides common configuration properties and parsing functionality for multipart requests, using a Map of Spring CommonsMultipartFile instances as representation of uploaded files and a String-based parameter Map as representation of uploaded form fields.

Since:
2.0
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected static class 
    Holder for a Map of Spring MultipartFiles and a Map of multipart parameters.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final org.apache.commons.logging.Log
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Instantiate a new CommonsFileUploadSupport with its corresponding FileItemFactory and FileUpload instances.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    cleanupFileItems(org.springframework.util.MultiValueMap<String,org.springframework.web.multipart.MultipartFile> multipartFiles)
    Cleanup the Spring MultipartFiles created during multipart parsing, potentially holding temporary data on disk.
    createMultipartFile(org.apache.tomcat.util.http.fileupload.FileItem fileItem)
    Create a CommonsMultipartFile wrapper for the given Commons FileItem.
    protected String
    Determine the default encoding to use for parsing requests.
    org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory
    Return the underlying org.apache.commons.fileupload.disk.DiskFileItemFactory instance.
    org.apache.tomcat.util.http.fileupload.FileUpload
    Return the underlying org.apache.commons.fileupload.FileUpload instance.
    protected boolean
    Return the temporary directory where uploaded files get stored.
    protected org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory
    Factory method for a Commons DiskFileItemFactory instance.
    protected abstract org.apache.tomcat.util.http.fileupload.FileUpload
    newFileUpload(org.apache.tomcat.util.http.fileupload.FileItemFactory fileItemFactory)
    Factory method for a Commons FileUpload instance.
    parseFileItems(List<org.apache.tomcat.util.http.fileupload.FileItem> fileItems, String encoding)
    Parse the given List of Commons FileItems into a Spring MultipartParsingResult, containing Spring MultipartFile instances and a Map of multipart parameter.
    protected org.apache.tomcat.util.http.fileupload.FileUpload
    Determine an appropriate FileUpload instance for the given encoding.
    void
    setDefaultEncoding(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 an upload gets rejected.
    void
    setMaxUploadSizePerFile(long maxUploadSizePerFile)
    Set the maximum allowed size (in bytes) for each individual file before an upload gets rejected.
    void
    setPreserveFilename(boolean preserveFilename)
    Set whether to preserve the filename as sent by the client, not stripping off path information in CommonsMultipartFile.getOriginalFilename().
    void
    setUploadTempDir(org.springframework.core.io.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 Details

    • logger

      protected final org.apache.commons.logging.Log logger
  • Constructor Details

  • Method Details

    • getFileItemFactory

      public org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory getFileItemFactory()
      Return the underlying org.apache.commons.fileupload.disk.DiskFileItemFactory instance. There is hardly any need to access this.
      Returns:
      the underlying DiskFileItemFactory instance
    • getFileUpload

      public org.apache.tomcat.util.http.fileupload.FileUpload getFileUpload()
      Return the underlying org.apache.commons.fileupload.FileUpload instance. There is hardly any need to access this.
      Returns:
      the underlying FileUpload instance
    • setMaxUploadSize

      public void setMaxUploadSize(long maxUploadSize)
      Set the maximum allowed size (in bytes) before an upload gets rejected. -1 indicates no limit (the default).
      Parameters:
      maxUploadSize - the maximum upload size allowed
      See Also:
      • invalid reference
        org.apache.commons.fileupload.FileUploadBase#setSizeMax
    • setMaxUploadSizePerFile

      public void setMaxUploadSizePerFile(long maxUploadSizePerFile)
      Set the maximum allowed size (in bytes) for each individual file before an upload gets rejected. -1 indicates no limit (the default).
      Parameters:
      maxUploadSizePerFile - the maximum upload size per file
      Since:
      4.2
      See Also:
      • invalid reference
        org.apache.commons.fileupload.FileUploadBase#setFileSizeMax
    • 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:
      • invalid reference
        org.apache.commons.fileupload.disk.DiskFileItemFactory#setSizeThreshold
    • setDefaultEncoding

      public void setDefaultEncoding(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:
      • invalid reference
        javax.servlet.ServletRequest#getCharacterEncoding
      • invalid reference
        javax.servlet.ServletRequest#setCharacterEncoding
      • WebUtils.DEFAULT_CHARACTER_ENCODING
      • invalid reference
        org.apache.commons.fileupload.FileUploadBase#setHeaderEncoding
    • getDefaultEncoding

      protected String getDefaultEncoding()
      Determine the default encoding to use for parsing requests.
      See Also:
    • setUploadTempDir

      public void setUploadTempDir(org.springframework.core.io.Resource uploadTempDir) throws IOException
      Set the temporary directory where uploaded files get stored. Default is the servlet container's temporary directory for the web application.
      Throws:
      IOException
      See Also:
      • WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE
    • isUploadTempDirSpecified

      protected boolean isUploadTempDirSpecified()
      Return the temporary directory where uploaded files get stored.
      See Also:
    • setPreserveFilename

      public void setPreserveFilename(boolean preserveFilename)
      Set whether to preserve the filename as sent by the client, not stripping off path information in CommonsMultipartFile.getOriginalFilename().

      Default is "false", stripping off path information that may prefix the actual filename e.g. from Opera. Switch this to "true" for preserving the client-specified filename as-is, including potential path separators.

      Since:
      4.3.5
      See Also:
    • newFileItemFactory

      protected org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory newFileItemFactory()
      Factory method for a Commons DiskFileItemFactory instance.

      Default implementation returns a standard DiskFileItemFactory. Can be overridden to use a custom subclass, e.g. for testing purposes.

      Returns:
      the new DiskFileItemFactory instance
    • newFileUpload

      protected abstract org.apache.tomcat.util.http.fileupload.FileUpload newFileUpload(org.apache.tomcat.util.http.fileupload.FileItemFactory fileItemFactory)
      Factory method for a Commons FileUpload instance.

      To be implemented by subclasses.

      Parameters:
      fileItemFactory - the Commons FileItemFactory to build upon
      Returns:
      the Commons FileUpload instance
    • prepareFileUpload

      protected org.apache.tomcat.util.http.fileupload.FileUpload prepareFileUpload(@Nullable String encoding)
      Determine an appropriate FileUpload instance for the given encoding.

      Default implementation returns the shared FileUpload instance if the encoding matches, else creates a new FileUpload instance with the same configuration other than the desired encoding.

      Parameters:
      encoding - the character encoding to use
      Returns:
      an appropriate FileUpload instance.
    • parseFileItems

      protected CommonsFileUploadSupport.MultipartParsingResult parseFileItems(List<org.apache.tomcat.util.http.fileupload.FileItem> fileItems, String encoding)
      Parse the given List of Commons FileItems into a Spring MultipartParsingResult, containing Spring MultipartFile instances and a Map of multipart parameter.
      Parameters:
      fileItems - the Commons FileItems to parse
      encoding - the encoding to use for form fields
      Returns:
      the Spring MultipartParsingResult
      See Also:
    • createMultipartFile

      protected CommonsMultipartFile createMultipartFile(org.apache.tomcat.util.http.fileupload.FileItem fileItem)
      Create a CommonsMultipartFile wrapper for the given Commons FileItem.
      Parameters:
      fileItem - the Commons FileItem to wrap
      Returns:
      the corresponding CommonsMultipartFile (potentially a custom subclass)
      Since:
      4.3.5
      See Also:
    • cleanupFileItems

      protected void cleanupFileItems(org.springframework.util.MultiValueMap<String,org.springframework.web.multipart.MultipartFile> multipartFiles)
      Cleanup the Spring MultipartFiles created during multipart parsing, potentially holding temporary data on disk.

      Deletes the underlying Commons FileItem instances.

      Parameters:
      multipartFiles - a Collection of MultipartFile instances
      See Also:
      • invalid reference
        org.apache.commons.fileupload.FileItem#delete()