DiskFileItemFactory.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 6k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2001-2004 The Apache Software Foundation
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package net.jforum.util.legacy.commons.fileupload.disk;
  17. import java.io.File;
  18. import net.jforum.util.legacy.commons.fileupload.FileItem;
  19. import net.jforum.util.legacy.commons.fileupload.FileItemFactory;
  20. /**
  21.  * <p>The default {@link org.apache.commons.fileupload.FileItemFactory}
  22.  * implementation. This implementation creates
  23.  * {@link org.apache.commons.fileupload.FileItem} instances which keep their
  24.  * content either in memory, for smaller items, or in a temporary file on disk,
  25.  * for larger items. The size threshold, above which content will be stored on
  26.  * disk, is configurable, as is the directory in which temporary files will be
  27.  * created.</p>
  28.  *
  29.  * <p>If not otherwise configured, the default configuration values are as
  30.  * follows:
  31.  * <ul>
  32.  *   <li>Size threshold is 10KB.</li>
  33.  *   <li>Repository is the system default temp directory, as returned by
  34.  *       <code>System.getProperty("java.io.tmpdir")</code>.</li>
  35.  * </ul>
  36.  * </p>
  37.  *
  38.  * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
  39.  *
  40.  * @since FileUpload 1.1
  41.  *
  42.  * @version $Id: DiskFileItemFactory.java,v 1.3 2005/07/26 03:05:02 rafaelsteil Exp $
  43.  */
  44. public class DiskFileItemFactory implements FileItemFactory {
  45.     // ----------------------------------------------------- Manifest constants
  46.     /**
  47.      * The default threshold above which uploads will be stored on disk.
  48.      */
  49.     public static final int DEFAULT_SIZE_THRESHOLD = 10240;
  50.     // ----------------------------------------------------- Instance Variables
  51.     /**
  52.      * The directory in which uploaded files will be stored, if stored on disk.
  53.      */
  54.     private File repository;
  55.     /**
  56.      * The threshold above which uploads will be stored on disk.
  57.      */
  58.     private int sizeThreshold = DEFAULT_SIZE_THRESHOLD;
  59.     // ----------------------------------------------------------- Constructors
  60.     /**
  61.      * Constructs an unconfigured instance of this class. The resulting factory
  62.      * may be configured by calling the appropriate setter methods.
  63.      */
  64.     public DiskFileItemFactory() {
  65.     }
  66.     /**
  67.      * Constructs a preconfigured instance of this class.
  68.      *
  69.      * @param sizeThreshold The threshold, in bytes, below which items will be
  70.      *                      retained in memory and above which they will be
  71.      *                      stored as a file.
  72.      * @param repository    The data repository, which is the directory in
  73.      *                      which files will be created, should the item size
  74.      *                      exceed the threshold.
  75.      */
  76.     public DiskFileItemFactory(int sizeThreshold, File repository) {
  77.         this.sizeThreshold = sizeThreshold;
  78.         this.repository = repository;
  79.     }
  80.     // ------------------------------------------------------------- Properties
  81.     /**
  82.      * Returns the directory used to temporarily store files that are larger
  83.      * than the configured size threshold.
  84.      *
  85.      * @return The directory in which temporary files will be located.
  86.      *
  87.      * @see #setRepository(java.io.File)
  88.      *
  89.      */
  90.     public File getRepository() {
  91.         return repository;
  92.     }
  93.     /**
  94.      * Sets the directory used to temporarily store files that are larger
  95.      * than the configured size threshold.
  96.      *
  97.      * @param repository The directory in which temporary files will be located.
  98.      *
  99.      * @see #getRepository()
  100.      *
  101.      */
  102.     public void setRepository(File repository) {
  103.         this.repository = repository;
  104.     }
  105.     /**
  106.      * Returns the size threshold beyond which files are written directly to
  107.      * disk. The default value is 1024 bytes.
  108.      *
  109.      * @return The size threshold, in bytes.
  110.      *
  111.      * @see #setSizeThreshold(int)
  112.      */
  113.     public int getSizeThreshold() {
  114.         return sizeThreshold;
  115.     }
  116.     /**
  117.      * Sets the size threshold beyond which files are written directly to disk.
  118.      *
  119.      * @param sizeThreshold The size threshold, in bytes.
  120.      *
  121.      * @see #getSizeThreshold()
  122.      *
  123.      */
  124.     public void setSizeThreshold(int sizeThreshold) {
  125.         this.sizeThreshold = sizeThreshold;
  126.     }
  127.     // --------------------------------------------------------- Public Methods
  128.     /**
  129.      * Create a new {@link org.apache.commons.fileupload.disk.DiskFileItem}
  130.      * instance from the supplied parameters and the local factory
  131.      * configuration.
  132.      *
  133.      * @param fieldName   The name of the form field.
  134.      * @param contentType The content type of the form field.
  135.      * @param isFormField <code>true</code> if this is a plain form field;
  136.      *                    <code>false</code> otherwise.
  137.      * @param fileName    The name of the uploaded file, if any, as supplied
  138.      *                    by the browser or other client.
  139.      *
  140.      * @return The newly created file item.
  141.      */
  142.     public FileItem createItem(
  143.             String fieldName,
  144.             String contentType,
  145.             boolean isFormField,
  146.             String fileName
  147.             ) {
  148.         return new DiskFileItem(fieldName, contentType,
  149.                 isFormField, fileName, sizeThreshold, repository);
  150.     }
  151. }