public abstract class CommonsFileUploadSupport extends Object
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.
Subclasses implement concrete resolution strategies for Servlet or Portlet environments: see CommonsMultipartResolver and CommonsPortletMultipartResolver, respectively. This base class is not tied to either of those APIs, factoring out common functionality.
CommonsMultipartFile
,
CommonsMultipartResolver
,
CommonsPortletMultipartResolver
Modifier and Type | Class and Description |
---|---|
protected static class |
CommonsFileUploadSupport.MultipartParsingResult
Holder for a Map of Spring MultipartFiles and a Map of
multipart parameters.
|
Constructor and Description |
---|
CommonsFileUploadSupport()
Instantiate a new CommonsFileUploadSupport with its
corresponding FileItemFactory and FileUpload instances.
|
Modifier and Type | Method and Description |
---|---|
protected void |
cleanupFileItems(MultiValueMap<String,MultipartFile> multipartFiles)
Cleanup the Spring MultipartFiles created during multipart parsing,
potentially holding temporary data on disk.
|
protected String |
getDefaultEncoding() |
org.apache.commons.fileupload.disk.DiskFileItemFactory |
getFileItemFactory()
Return the underlying
org.apache.commons.fileupload.disk.DiskFileItemFactory
instance. |
org.apache.commons.fileupload.FileUpload |
getFileUpload()
Return the underlying
org.apache.commons.fileupload.FileUpload
instance. |
protected boolean |
isUploadTempDirSpecified() |
protected org.apache.commons.fileupload.disk.DiskFileItemFactory |
newFileItemFactory()
Factory method for a Commons DiskFileItemFactory instance.
|
protected abstract org.apache.commons.fileupload.FileUpload |
newFileUpload(org.apache.commons.fileupload.FileItemFactory fileItemFactory)
Factory method for a Commons FileUpload instance.
|
protected CommonsFileUploadSupport.MultipartParsingResult |
parseFileItems(List<org.apache.commons.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.commons.fileupload.FileUpload |
prepareFileUpload(String encoding)
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 uploads are refused.
|
void |
setUploadTempDir(Resource uploadTempDir)
Set the temporary directory where uploaded files get stored.
|
protected final Log logger
public CommonsFileUploadSupport()
public org.apache.commons.fileupload.disk.DiskFileItemFactory getFileItemFactory()
org.apache.commons.fileupload.disk.DiskFileItemFactory
instance. There is hardly any need to access this.public org.apache.commons.fileupload.FileUpload getFileUpload()
org.apache.commons.fileupload.FileUpload
instance. There is hardly any need to access this.public void setMaxUploadSize(long maxUploadSize)
maxUploadSize
- the maximum upload size allowedFileUploadBase.setSizeMax(long)
public void setMaxInMemorySize(int maxInMemorySize)
maxInMemorySize
- the maximum in memory size allowedDiskFileItemFactory.setSizeThreshold(int)
public void setDefaultEncoding(String defaultEncoding)
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.
defaultEncoding
- the character encoding to useServletRequest.getCharacterEncoding()
,
ServletRequest.setCharacterEncoding(java.lang.String)
,
WebUtils.DEFAULT_CHARACTER_ENCODING
,
FileUploadBase.setHeaderEncoding(java.lang.String)
protected String getDefaultEncoding()
public void setUploadTempDir(Resource uploadTempDir) throws IOException
IOException
WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE
protected boolean isUploadTempDirSpecified()
protected org.apache.commons.fileupload.disk.DiskFileItemFactory newFileItemFactory()
Default implementation returns a standard DiskFileItemFactory. Can be overridden to use a custom subclass, e.g. for testing purposes.
protected abstract org.apache.commons.fileupload.FileUpload newFileUpload(org.apache.commons.fileupload.FileItemFactory fileItemFactory)
To be implemented by subclasses.
fileItemFactory
- the Commons FileItemFactory to build uponprotected org.apache.commons.fileupload.FileUpload prepareFileUpload(String 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.
encoding
- the character encoding to useprotected CommonsFileUploadSupport.MultipartParsingResult parseFileItems(List<org.apache.commons.fileupload.FileItem> fileItems, String encoding)
fileItems
- the Commons FileIterms to parseencoding
- the encoding to use for form fieldsCommonsMultipartFile.CommonsMultipartFile(org.apache.commons.fileupload.FileItem)
protected void cleanupFileItems(MultiValueMap<String,MultipartFile> multipartFiles)
Deletes the underlying Commons FileItem instances.
multipartFiles
- Collection of MultipartFile instancesFileItem.delete()