Class DiskFileItemFactory
- All Implemented Interfaces:
FileItemFactory<DiskFileItem>
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 10 KB.
- Repository is the system default temporary directory, as returned by
System.getProperty("java.io.tmpdir").
NOTE: Files are created in the system default temporary 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, AbstractOriginSupplier.setPath(Path)
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.
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface org.apache.commons.fileupload2.core.FileItemFactory
FileItemFactory.AbstractFileItemBuilder<I extends FileItem<I>,B extends FileItemFactory.AbstractFileItemBuilder<I, B>> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CharsetDefault content Charset to be used when no explicit Charset parameter is provided by the sender.static final intThe default threshold in bytes above which uploads will be stored on disk.private final org.apache.commons.io.FileCleaningTrackerThe instance ofFileCleaningTracker, which is responsible for deleting temporary files.private final PathThe directory in which uploaded files will be stored, if stored on disk.private final intThe threshold above which uploads will be stored on disk. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateDiskFileItemFactory(Path repository, int threshold, Charset charsetDefault, org.apache.commons.io.FileCleaningTracker fileCleaningTracker) Constructs a preconfigured instance of this class. -
Method Summary
Modifier and TypeMethodDescriptionstatic DiskFileItemFactory.Builderbuilder()Constructs a newDiskFileItemFactory.Builder.Creates a new AbstractFileItemBuilder.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.
-
Field Details
-
DEFAULT_THRESHOLD
public static final int DEFAULT_THRESHOLDThe default threshold in bytes above which uploads will be stored on disk.- See Also:
-
repository
The directory in which uploaded files will be stored, if stored on disk. -
threshold
private final int thresholdThe threshold above which uploads will be stored on disk. -
fileCleaningTracker
private final org.apache.commons.io.FileCleaningTracker fileCleaningTrackerThe instance ofFileCleaningTracker, which is responsible for deleting temporary files.May be null, if tracking files is not required.
-
charsetDefault
Default content Charset to be used when no explicit Charset parameter is provided by the sender.
-
-
Constructor Details
-
DiskFileItemFactory
private DiskFileItemFactory(Path repository, int threshold, Charset charsetDefault, org.apache.commons.io.FileCleaningTracker fileCleaningTracker) Constructs a preconfigured instance of this class.- Parameters:
repository- The data repository, which is the directory in which files will be created, should the item size exceed the threshold.threshold- The threshold, in bytes, below which items will be retained in memory and above which they will be stored as a file.charsetDefault- Sets the default charset for use when no explicit charset parameter is provided by the sender.fileCleaningTracker- Callback to track files created, or null (default) to disable tracking.
-
-
Method Details
-
builder
Constructs a newDiskFileItemFactory.Builder.- Returns:
- a new
DiskFileItemFactory.Builder.
-
fileItemBuilder
Description copied from interface:FileItemFactoryCreates a new AbstractFileItemBuilder.- Specified by:
fileItemBuilderin interfaceFileItemFactory<DiskFileItem>- Returns:
- a new AbstractFileItemBuilder.
-
getCharsetDefault
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.
-
getThreshold
public int getThreshold()Gets the size threshold beyond which files are written directly to disk. The default value is 10240 bytes.- Returns:
- The size threshold in bytes.
-