Class DiskFileItemFactory
- All Implemented Interfaces:
FileItemFactory
- Direct Known Subclasses:
DefaultFileItemFactory
The default FileItemFactory
implementation. This implementation creates
FileItem instances which keep their
content either in memory, for smaller items, or in a temporary file on disk,
for larger items. The size threshold, above which content will be stored on
disk, is configurable, as is the directory in which temporary files will be
created.
If not otherwise configured, the default configuration values are as follows:
- Size threshold is 10KB.
- Repository is the system default temp directory, as returned by
System.getProperty("java.io.tmpdir").
NOTE: Files are created in the system default temp directory with
predictable names. This means that a local attacker with write access to that
directory can perform a TOUTOC attack to replace any uploaded file with a
file of the attackers choice. The implications of this will depend on how the
uploaded file is used but could be significant. When using this
implementation in an environment with local, untrusted users,
setRepository(File) MUST be used to configure a repository location
that is not publicly writable. In a Servlet container the location identified
by the ServletContext attribute javax.servlet.context.tempdir
may be used.
Temporary files, which are created for file items, should be
deleted later on. The best way to do this is using a
FileCleaningTracker, which you can set on the
DiskFileItemFactory. However, if you do use such a tracker,
then you must consider the following: Temporary files are automatically
deleted as soon as they are no longer needed. (More precisely, when the
corresponding instance of File is garbage collected.)
This is done by the so-called reaper thread, which is started and stopped
automatically by the FileCleaningTracker when there are files to be
tracked.
It might make sense to terminate that thread, for example, if
your web application ends. See the section on "Resource cleanup"
in the users guide of commons-fileupload.
- Since:
- FileUpload 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default threshold above which uploads will be stored on disk.private StringDefault content charset to be used when no explicit charset parameter is provided by the sender.private org.apache.commons.io.FileCleaningTrackerThe instance ofFileCleaningTracker, which is responsible for deleting temporary files.private FileThe directory in which uploaded files will be stored, if stored on disk.private intThe threshold above which uploads will be stored on disk. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an unconfigured instance of this class.DiskFileItemFactory(int sizeThreshold, File repository) Constructs a preconfigured instance of this class. -
Method Summary
Modifier and TypeMethodDescriptioncreateItem(String fieldName, String contentType, boolean isFormField, String fileName) Create a newDiskFileIteminstance from the supplied parameters and the local factory configuration.Gets the default charset for use when no explicit charset parameter is provided by the sender.org.apache.commons.io.FileCleaningTrackerGets the tracker, which is responsible for deleting temporary files.Gets the directory used to temporarily store files that are larger than the configured size threshold.intGets the size threshold beyond which files are written directly to disk.voidsetDefaultCharset(String charset) Sets the default charset for use when no explicit charset parameter is provided by the sender.voidsetFileCleaningTracker(org.apache.commons.io.FileCleaningTracker fileCleaningTracker) Sets the tracker, which is responsible for deleting temporary files.voidsetRepository(File repository) Sets the directory used to temporarily store files that are larger than the configured size threshold.voidsetSizeThreshold(int sizeThreshold) Sets the size threshold beyond which files are written directly to disk.
-
Field Details
-
DEFAULT_SIZE_THRESHOLD
public static final int DEFAULT_SIZE_THRESHOLDThe default threshold above which uploads will be stored on disk.- See Also:
-
repository
The directory in which uploaded files will be stored, if stored on disk. -
sizeThreshold
private int sizeThresholdThe threshold above which uploads will be stored on disk. -
fileCleaningTracker
private org.apache.commons.io.FileCleaningTracker fileCleaningTrackerThe instance of
FileCleaningTracker, which is responsible for deleting temporary files.May be null, if tracking files is not required.
-
defaultCharset
Default content charset to be used when no explicit charset parameter is provided by the sender.
-
-
Constructor Details
-
DiskFileItemFactory
public DiskFileItemFactory()Constructs an unconfigured instance of this class. The resulting factory may be configured by calling the appropriate setter methods. -
DiskFileItemFactory
Constructs a preconfigured instance of this class.- Parameters:
sizeThreshold- The threshold, in bytes, below which items will be retained in memory and above which they will be stored as a file.repository- The data repository, which is the directory in which files will be created, should the item size exceed the threshold.
-
-
Method Details
-
createItem
public FileItem createItem(String fieldName, String contentType, boolean isFormField, String fileName) Create a newDiskFileIteminstance from the supplied parameters and the local factory configuration.- Specified by:
createItemin interfaceFileItemFactory- Parameters:
fieldName- The name of the form field.contentType- The content type of the form field.isFormField-trueif this is a plain form field;falseotherwise.fileName- The name of the uploaded file, if any, as supplied by the browser or other client.- Returns:
- The newly created file item.
-
getDefaultCharset
Gets the default charset for use when no explicit charset parameter is provided by the sender.- Returns:
- the default charset
-
getFileCleaningTracker
public org.apache.commons.io.FileCleaningTracker getFileCleaningTracker()Gets the tracker, which is responsible for deleting temporary files.- Returns:
- An instance of
FileCleaningTracker, or null (default), if temporary files aren't tracked.
-
getRepository
Gets the directory used to temporarily store files that are larger than the configured size threshold.- Returns:
- The directory in which temporary files will be located.
- See Also:
-
getSizeThreshold
public int getSizeThreshold()Gets the size threshold beyond which files are written directly to disk. The default value is 10240 bytes.- Returns:
- The size threshold, in bytes.
- See Also:
-
setDefaultCharset
Sets the default charset for use when no explicit charset parameter is provided by the sender.- Parameters:
charset- the default charset
-
setFileCleaningTracker
public void setFileCleaningTracker(org.apache.commons.io.FileCleaningTracker fileCleaningTracker) Sets the tracker, which is responsible for deleting temporary files.- Parameters:
fileCleaningTracker- An instance ofFileCleaningTracker, which will from now on track the created files, or null (default), to disable tracking.
-
setRepository
Sets the directory used to temporarily store files that are larger than the configured size threshold.- Parameters:
repository- The directory in which temporary files will be located.- See Also:
-
setSizeThreshold
public void setSizeThreshold(int sizeThreshold) Sets the size threshold beyond which files are written directly to disk.- Parameters:
sizeThreshold- The size threshold, in bytes.- See Also:
-