You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by jo...@apache.org on 2025/07/13 21:43:11 UTC
(commons-fileupload) branch master updated: FILEUPLOAD-295: Updating the Javadocs.
This is an automated email from the ASF dual-hosted git repository.
jochen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git
The following commit(s) were added to refs/heads/master by this push:
new b62bd5a0 FILEUPLOAD-295: Updating the Javadocs.
b62bd5a0 is described below
commit b62bd5a088e95a2da0bf2152310467eb396b0411
Author: Jochen Wiedmann <jo...@gmail.com>
AuthorDate: Sun Jul 13 23:43:03 2025 +0200
FILEUPLOAD-295: Updating the Javadocs.
---
.../commons/fileupload2/core/DiskFileItem.java | 57 +++++++++++++---------
.../fileupload2/core/DiskFileItemFactory.java | 10 +++-
2 files changed, 42 insertions(+), 25 deletions(-)
diff --git a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/DiskFileItem.java b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/DiskFileItem.java
index db1aed2f..4a526ffe 100644
--- a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/DiskFileItem.java
+++ b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/DiskFileItem.java
@@ -46,21 +46,46 @@ import org.apache.commons.io.file.PathUtils;
/**
* The default implementation of the {@link FileItem FileItem} interface.
- * <p>
- * After retrieving an instance of this class from a {@link DiskFileItemFactory} instance (see
+ *
+ * <p>After retrieving an instance of this class from a {@link DiskFileItemFactory} instance (see
* {@code org.apache.commons.fileupload2.core.servlet.ServletFileUpload
* #parseRequest(javax.servlet.http.HttpServletRequest)}), you may either request all contents of file at once using {@link #get()} or request an
* {@link java.io.InputStream InputStream} with {@link #getInputStream()} and process the file without attempting to load it into memory, which may come handy
- * with large files.
- * </p>
- * <p>
- * Temporary files, which are created for file items, should be deleted later on. The best way to do this is using a
+ * with large files.</p>
+ *
+ * <p><em>State model</em>: Instances of {@link DiskFileItem} are subject to a carefully designed state model.
+ * Depending on the so-called {@link #getThreshold() threshold}, either of the three models are possible:</p>
+ * <ol>
+ * <li><em>threshold = -1</em>
+ * Uploaded data is never kept in memory. Instead, a temporary file is being created immediately.
+ *
+ * {@link #isInMemory()} will always return false, {@link #getPath()} will always return the path
+ * of an existing file. The temporary file may be empty.</li>
+ * <li><em>threshold = 0</em>
+ * Uploaded data is never kept in memory. (Same as threshold=-1.) However, the temporary file is
+ * only created, if data was uploaded. Or, in other words: The uploaded file will never be
+ * empty.
+ *
+ * {@link #isInMemory()} will return true, if no data was uploaded, otherwise it will be false.
+ * In the former case {@link #getPath()} will return null, but in the latter case it returns
+ * the path of an existing, non-empty file.</li>
+ * <li><em>threshold > 0</em>
+ * Uploaded data will be kept in memory, if the size is below the threshold. If the size
+ * is equal to, or above the threshold, then a temporary file has been created, and all
+ * uploaded data has been transferred to that file.
+ *
+ * {@link #isInMemory()} returns true, if the size of the uploaded data is below the threshold.
+ * If so, {@link #getPath()} returns null. Otherwise, {@link #isInMemory()} returns false,
+ * and {@link #getPath()} returns the path of an existing, temporary file. The size
+ * of the temporary file is equal to, or above the threshold.</li>
+ * </ol>
+ *
+ * <p>Temporary files, which are created for file items, should be deleted later on. The best way to do this is using a
* {@link org.apache.commons.io.FileCleaningTracker}, which you can set on the {@link 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
* {@link java.io.File} is garbage collected.) This is done by the so-called reaper thread, which is started and stopped automatically by the
* {@link org.apache.commons.io.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.
- * </p>
+ * application ends. See the section on "Resource cleanup" in the users guide of Commons FileUpload.</p>
*/
public final class DiskFileItem implements FileItem<DiskFileItem> {
@@ -559,22 +584,6 @@ public final class DiskFileItem implements FileItem<DiskFileItem> {
return new String(get(), Charsets.toCharset(charset, charsetDefault));
}
- /**
- * Creates and returns a {@link java.io.File File} representing a uniquely named temporary file in the configured repository path. The lifetime of the file
- * is tied to the lifetime of the {@code FileItem} instance; the file will be deleted when the instance is garbage collected.
- * <p>
- * <strong>Note: Subclasses that override this method must ensure that they return the same File each time.</strong>
- * </p>
- *
- * @return The {@link java.io.File File} to be used for temporary storage.
- */
- protected Path getTempFile() {
- if (dos != null) {
- return dos.getPath();
- }
- return null;
- }
-
/**
* Tests whether or not a {@code FileItem} instance represents a simple form field.
*
diff --git a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/DiskFileItemFactory.java b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/DiskFileItemFactory.java
index e2dea3aa..f61e4540 100644
--- a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/DiskFileItemFactory.java
+++ b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/DiskFileItemFactory.java
@@ -37,10 +37,14 @@ import org.apache.commons.io.file.PathUtils;
* <li>Size threshold is 10 KB.</li>
* <li>Repository is the system default temporary directory, as returned by {@code System.getProperty("java.io.tmpdir")}.</li>
* </ul>
+ * <p><em>State model</em>: The created instances of {@link DiskFileItem} are subject to a carefully designed state model,
+ * which is also controlled by the threshold. Therefore, it is strongly recommended to set the threshold explicitly, using
+ * {@link Builder#setThreshold(int)}. Details
+ * on the state model can be found {@link DiskFileItem here}.</p>
* <p>
* <strong>NOTE</strong>: 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,
+ * the uploaded file is used, but could be significant. When using this implementation in an environment with local, untrusted users,
* {@link Builder#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 {@code javax.servlet.context.tempdir} may be used.
* </p>
@@ -127,6 +131,10 @@ public final class DiskFileItemFactory implements FileItemFactory<DiskFileItem>
* a certain number of bytes (the threshold) is reached. At this point, the
* incoming data is transferred to a temporary file, and the in-memory data
* is removed.
+ *
+ * The threshold will also control the <em>state model</em> of the created
+ * instances of {@link DiskFileItem}. Details on the state model can be
+ * found {@link DiskFileItem here}.
* @param threshold The threshold, which is being used.
* @return This builder.
*/