public abstract class CommonsFileUploadSupport
extends java.lang.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.
CommonsMultipartFile
,
CommonsMultipartResolver
Modifier and Type | Class and Description |
---|---|
protected static class |
CommonsFileUploadSupport.MultipartParsingResult
Holder for a Map of Spring MultipartFiles and a Map of
multipart parameters.
|
Modifier and Type | Field and Description |
---|---|
private DiskFileItemFactory |
fileItemFactory |
private FileUpload |
fileUpload |
protected Log |
logger |
private boolean |
preserveFilename |
private boolean |
uploadTempDirSpecified |
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<java.lang.String,MultipartFile> multipartFiles)
Cleanup the Spring MultipartFiles created during multipart parsing,
potentially holding temporary data on disk.
|
protected CommonsMultipartFile |
createMultipartFile(FileItem fileItem)
Create a
CommonsMultipartFile wrapper for the given Commons FileItem . |
private java.lang.String |
determineEncoding(java.lang.String contentTypeHeader,
java.lang.String defaultEncoding) |
protected java.lang.String |
getDefaultEncoding()
Determine the default encoding to use for parsing requests.
|
DiskFileItemFactory |
getFileItemFactory()
Return the underlying
org.apache.commons.fileupload.disk.DiskFileItemFactory
instance. |
FileUpload |
getFileUpload()
Return the underlying
org.apache.commons.fileupload.FileUpload
instance. |
protected boolean |
isUploadTempDirSpecified()
Return the temporary directory where uploaded files get stored.
|
protected DiskFileItemFactory |
newFileItemFactory()
Factory method for a Commons DiskFileItemFactory instance.
|
protected abstract FileUpload |
newFileUpload(FileItemFactory fileItemFactory)
Factory method for a Commons FileUpload instance.
|
protected CommonsFileUploadSupport.MultipartParsingResult |
parseFileItems(java.util.List<FileItem> fileItems,
java.lang.String encoding)
Parse the given List of Commons FileItems into a Spring MultipartParsingResult,
containing Spring MultipartFile instances and a Map of multipart parameter.
|
protected FileUpload |
prepareFileUpload(java.lang.String encoding)
Determine an appropriate FileUpload instance for the given encoding.
|
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 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(Resource uploadTempDir)
Set the temporary directory where uploaded files get stored.
|
protected final Log logger
private final DiskFileItemFactory fileItemFactory
private final FileUpload fileUpload
private boolean uploadTempDirSpecified
private boolean preserveFilename
public CommonsFileUploadSupport()
public DiskFileItemFactory getFileItemFactory()
org.apache.commons.fileupload.disk.DiskFileItemFactory
instance. There is hardly any need to access this.public 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 allowedorg.apache.commons.fileupload.FileUploadBase#setSizeMax
public void setMaxUploadSizePerFile(long maxUploadSizePerFile)
maxUploadSizePerFile
- the maximum upload size per fileorg.apache.commons.fileupload.FileUploadBase#setFileSizeMax
public void setMaxInMemorySize(int maxInMemorySize)
maxInMemorySize
- the maximum in memory size allowedorg.apache.commons.fileupload.disk.DiskFileItemFactory#setSizeThreshold
public void setDefaultEncoding(java.lang.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 usejavax.servlet.ServletRequest#getCharacterEncoding
,
javax.servlet.ServletRequest#setCharacterEncoding
,
WebUtils.DEFAULT_CHARACTER_ENCODING
,
org.apache.commons.fileupload.FileUploadBase#setHeaderEncoding
protected java.lang.String getDefaultEncoding()
setDefaultEncoding(java.lang.String)
public void setUploadTempDir(Resource uploadTempDir) throws java.io.IOException
java.io.IOException
WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE
protected boolean isUploadTempDirSpecified()
public void setPreserveFilename(boolean preserveFilename)
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.
MultipartFile.getOriginalFilename()
,
CommonsMultipartFile.setPreserveFilename(boolean)
protected DiskFileItemFactory newFileItemFactory()
Default implementation returns a standard DiskFileItemFactory. Can be overridden to use a custom subclass, e.g. for testing purposes.
protected abstract FileUpload newFileUpload(FileItemFactory fileItemFactory)
To be implemented by subclasses.
fileItemFactory
- the Commons FileItemFactory to build uponprotected FileUpload prepareFileUpload(@Nullable java.lang.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(java.util.List<FileItem> fileItems, java.lang.String encoding)
fileItems
- the Commons FileItems to parseencoding
- the encoding to use for form fieldsCommonsMultipartFile#CommonsMultipartFile(org.apache.commons.fileupload.FileItem)
protected CommonsMultipartFile createMultipartFile(FileItem fileItem)
CommonsMultipartFile
wrapper for the given Commons FileItem
.fileItem
- the Commons FileItem to wrapsetPreserveFilename(boolean)
,
CommonsMultipartFile.setPreserveFilename(boolean)
protected void cleanupFileItems(MultiValueMap<java.lang.String,MultipartFile> multipartFiles)
Deletes the underlying Commons FileItem instances.
multipartFiles
- a Collection of MultipartFile instancesorg.apache.commons.fileupload.FileItem#delete()
private java.lang.String determineEncoding(java.lang.String contentTypeHeader, java.lang.String defaultEncoding)