View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.fileupload.disk;
18  
19  import java.io.File;
20  
21  import org.apache.commons.fileupload.FileItem;
22  import org.apache.commons.fileupload.FileItemFactory;
23  
24  /**
25   * <p>The default {@link org.apache.commons.fileupload.FileItemFactory}
26   * implementation. This implementation creates
27   * {@link org.apache.commons.fileupload.FileItem} instances which keep their
28   * content either in memory, for smaller items, or in a temporary file on disk,
29   * for larger items. The size threshold, above which content will be stored on
30   * disk, is configurable, as is the directory in which temporary files will be
31   * created.</p>
32   *
33   * <p>If not otherwise configured, the default configuration values are as
34   * follows:
35   * <ul>
36   *   <li>Size threshold is 10KB.</li>
37   *   <li>Repository is the system default temp directory, as returned by
38   *       <code>System.getProperty("java.io.tmpdir")</code>.</li>
39   * </ul>
40   * </p>
41   *
42   * <p>When using the <code>DiskFileItemFactory</code>, then you should
43   * consider the following: Temporary files are automatically deleted as
44   * soon as they are no longer needed. (More precisely, when the
45   * corresponding instance of {@link java.io.File} is garbage collected.)
46   * This is done by the so-called reaper thread, which is started
47   * automatically when the class {@link org.apache.commons.io.FileCleaner}
48   * is loaded. It might make sense to terminate that thread, for example,
49   * if your web application ends. See the section on "Resource cleanup"
50   * in the users guide of commons-fileupload.</p>
51   *
52   * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
53   *
54   * @since FileUpload 1.1
55   *
56   * @version $Id: DiskFileItemFactory.java 502350 2007-02-01 20:42:48Z jochen $
57   */
58  public class DiskFileItemFactory implements FileItemFactory {
59  
60      // ----------------------------------------------------- Manifest constants
61  
62  
63      /**
64       * The default threshold above which uploads will be stored on disk.
65       */
66      public static final int DEFAULT_SIZE_THRESHOLD = 10240;
67  
68  
69      // ----------------------------------------------------- Instance Variables
70  
71  
72      /**
73       * The directory in which uploaded files will be stored, if stored on disk.
74       */
75      private File repository;
76  
77  
78      /**
79       * The threshold above which uploads will be stored on disk.
80       */
81      private int sizeThreshold = DEFAULT_SIZE_THRESHOLD;
82  
83  
84      // ----------------------------------------------------------- Constructors
85  
86  
87      /**
88       * Constructs an unconfigured instance of this class. The resulting factory
89       * may be configured by calling the appropriate setter methods.
90       */
91      public DiskFileItemFactory() {
92          // Does nothing.
93      }
94  
95  
96      /**
97       * Constructs a preconfigured instance of this class.
98       *
99       * @param sizeThreshold The threshold, in bytes, below which items will be
100      *                      retained in memory and above which they will be
101      *                      stored as a file.
102      * @param repository    The data repository, which is the directory in
103      *                      which files will be created, should the item size
104      *                      exceed the threshold.
105      */
106     public DiskFileItemFactory(int sizeThreshold, File repository) {
107         this.sizeThreshold = sizeThreshold;
108         this.repository = repository;
109     }
110 
111 
112     // ------------------------------------------------------------- Properties
113 
114 
115     /**
116      * Returns the directory used to temporarily store files that are larger
117      * than the configured size threshold.
118      *
119      * @return The directory in which temporary files will be located.
120      *
121      * @see #setRepository(java.io.File)
122      *
123      */
124     public File getRepository() {
125         return repository;
126     }
127 
128 
129     /**
130      * Sets the directory used to temporarily store files that are larger
131      * than the configured size threshold.
132      *
133      * @param repository The directory in which temporary files will be located.
134      *
135      * @see #getRepository()
136      *
137      */
138     public void setRepository(File repository) {
139         this.repository = repository;
140     }
141 
142 
143     /**
144      * Returns the size threshold beyond which files are written directly to
145      * disk. The default value is 10240 bytes.
146      *
147      * @return The size threshold, in bytes.
148      *
149      * @see #setSizeThreshold(int)
150      */
151     public int getSizeThreshold() {
152         return sizeThreshold;
153     }
154 
155 
156     /**
157      * Sets the size threshold beyond which files are written directly to disk.
158      *
159      * @param sizeThreshold The size threshold, in bytes.
160      *
161      * @see #getSizeThreshold()
162      *
163      */
164     public void setSizeThreshold(int sizeThreshold) {
165         this.sizeThreshold = sizeThreshold;
166     }
167 
168 
169     // --------------------------------------------------------- Public Methods
170 
171     /**
172      * Create a new {@link org.apache.commons.fileupload.disk.DiskFileItem}
173      * instance from the supplied parameters and the local factory
174      * configuration.
175      *
176      * @param fieldName   The name of the form field.
177      * @param contentType The content type of the form field.
178      * @param isFormField <code>true</code> if this is a plain form field;
179      *                    <code>false</code> otherwise.
180      * @param fileName    The name of the uploaded file, if any, as supplied
181      *                    by the browser or other client.
182      *
183      * @return The newly created file item.
184      */
185     public FileItem createItem(
186             String fieldName,
187             String contentType,
188             boolean isFormField,
189             String fileName
190             ) {
191         return new DiskFileItem(fieldName, contentType,
192                 isFormField, fileName, sizeThreshold, repository);
193     }
194 
195 }