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 import org.apache.commons.io.FileCleaningTracker;
24
25
26 /**
27 * <p>The default {@link org.apache.commons.fileupload.FileItemFactory}
28 * implementation. This implementation creates
29 * {@link org.apache.commons.fileupload.FileItem} instances which keep their
30 * content either in memory, for smaller items, or in a temporary file on disk,
31 * for larger items. The size threshold, above which content will be stored on
32 * disk, is configurable, as is the directory in which temporary files will be
33 * created.</p>
34 *
35 * <p>If not otherwise configured, the default configuration values are as
36 * follows:
37 * <ul>
38 * <li>Size threshold is 10KB.</li>
39 * <li>Repository is the system default temp directory, as returned by
40 * <code>System.getProperty("java.io.tmpdir")</code>.</li>
41 * </ul>
42 * </p>
43 *
44 * <p>When using the <code>DiskFileItemFactory</code>, then you should
45 * consider the following: Temporary files are automatically deleted as
46 * soon as they are no longer needed. (More precisely, when the
47 * corresponding instance of {@link java.io.File} is garbage collected.)
48 * Cleaning up those files is done by an instance of
49 * {@link FileCleaningTracker}, and an associated thread. In a complex
50 * environment, for example in a web application, you should consider
51 * terminating this thread, for example, when your web application
52 * ends. See the section on "Resource cleanup"
53 * in the users guide of commons-fileupload.</p>
54 *
55 * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
56 *
57 * @since FileUpload 1.1
58 *
59 * @version $Id: DiskFileItemFactory.java 607869 2008-01-01 16:42:17Z jochen $
60 */
61 public class DiskFileItemFactory implements FileItemFactory {
62
63 // ----------------------------------------------------- Manifest constants
64
65
66 /**
67 * The default threshold above which uploads will be stored on disk.
68 */
69 public static final int DEFAULT_SIZE_THRESHOLD = 10240;
70
71
72 // ----------------------------------------------------- Instance Variables
73
74
75 /**
76 * The directory in which uploaded files will be stored, if stored on disk.
77 */
78 private File repository;
79
80
81 /**
82 * The threshold above which uploads will be stored on disk.
83 */
84 private int sizeThreshold = DEFAULT_SIZE_THRESHOLD;
85
86
87 /**
88 * <p>The instance of {@link FileCleaningTracker}, which is responsible
89 * for deleting temporary files.</p>
90 * <p>May be null, if tracking files is not required.</p>
91 */
92 private FileCleaningTracker fileCleaningTracker;
93
94 // ----------------------------------------------------------- Constructors
95
96
97 /**
98 * Constructs an unconfigured instance of this class. The resulting factory
99 * may be configured by calling the appropriate setter methods.
100 */
101 public DiskFileItemFactory() {
102 this(DEFAULT_SIZE_THRESHOLD, null);
103 }
104
105
106 /**
107 * Constructs a preconfigured instance of this class.
108 *
109 * @param sizeThreshold The threshold, in bytes, below which items will be
110 * retained in memory and above which they will be
111 * stored as a file.
112 * @param repository The data repository, which is the directory in
113 * which files will be created, should the item size
114 * exceed the threshold.
115 */
116 public DiskFileItemFactory(int sizeThreshold, File repository) {
117 this.sizeThreshold = sizeThreshold;
118 this.repository = repository;
119 }
120
121 // ------------------------------------------------------------- Properties
122
123
124 /**
125 * Returns the directory used to temporarily store files that are larger
126 * than the configured size threshold.
127 *
128 * @return The directory in which temporary files will be located.
129 *
130 * @see #setRepository(java.io.File)
131 *
132 */
133 public File getRepository() {
134 return repository;
135 }
136
137
138 /**
139 * Sets the directory used to temporarily store files that are larger
140 * than the configured size threshold.
141 *
142 * @param repository The directory in which temporary files will be located.
143 *
144 * @see #getRepository()
145 *
146 */
147 public void setRepository(File repository) {
148 this.repository = repository;
149 }
150
151
152 /**
153 * Returns the size threshold beyond which files are written directly to
154 * disk. The default value is 10240 bytes.
155 *
156 * @return The size threshold, in bytes.
157 *
158 * @see #setSizeThreshold(int)
159 */
160 public int getSizeThreshold() {
161 return sizeThreshold;
162 }
163
164
165 /**
166 * Sets the size threshold beyond which files are written directly to disk.
167 *
168 * @param sizeThreshold The size threshold, in bytes.
169 *
170 * @see #getSizeThreshold()
171 *
172 */
173 public void setSizeThreshold(int sizeThreshold) {
174 this.sizeThreshold = sizeThreshold;
175 }
176
177
178 // --------------------------------------------------------- Public Methods
179
180 /**
181 * Create a new {@link org.apache.commons.fileupload.disk.DiskFileItem}
182 * instance from the supplied parameters and the local factory
183 * configuration.
184 *
185 * @param fieldName The name of the form field.
186 * @param contentType The content type of the form field.
187 * @param isFormField <code>true</code> if this is a plain form field;
188 * <code>false</code> otherwise.
189 * @param fileName The name of the uploaded file, if any, as supplied
190 * by the browser or other client.
191 *
192 * @return The newly created file item.
193 */
194 public FileItem createItem(String fieldName, String contentType,
195 boolean isFormField, String fileName) {
196 DiskFileItem result = new DiskFileItem(fieldName, contentType,
197 isFormField, fileName, sizeThreshold, repository);
198 FileCleaningTracker tracker = getFileCleaningTracker();
199 if (tracker != null) {
200 tracker.track(result.getTempFile(), this);
201 }
202 return result;
203 }
204
205
206 /**
207 * Returns the tracker, which is responsible for deleting temporary
208 * files.
209 * @return An instance of {@link FileCleaningTracker}, defaults to
210 * {@link org.apache.commons.io.FileCleaner#getInstance()}. Null,
211 * if temporary files aren't tracked.
212 */
213 public FileCleaningTracker getFileCleaningTracker() {
214 return fileCleaningTracker;
215 }
216
217 /**
218 * Returns the tracker, which is responsible for deleting temporary
219 * files.
220 * @param pTracker An instance of {@link FileCleaningTracker},
221 * which will from now on track the created files. May be null
222 * to disable tracking.
223 */
224 public void setFileCleaningTracker(FileCleaningTracker pTracker) {
225 fileCleaningTracker = pTracker;
226 }
227 }