001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.fileupload.disk;
018:
019: import java.io.File;
020:
021: import org.apache.commons.fileupload.FileItem;
022: import org.apache.commons.fileupload.FileItemFactory;
023:
024: /**
025: * <p>The default {@link org.apache.commons.fileupload.FileItemFactory}
026: * implementation. This implementation creates
027: * {@link org.apache.commons.fileupload.FileItem} instances which keep their
028: * content either in memory, for smaller items, or in a temporary file on disk,
029: * for larger items. The size threshold, above which content will be stored on
030: * disk, is configurable, as is the directory in which temporary files will be
031: * created.</p>
032: *
033: * <p>If not otherwise configured, the default configuration values are as
034: * follows:
035: * <ul>
036: * <li>Size threshold is 10KB.</li>
037: * <li>Repository is the system default temp directory, as returned by
038: * <code>System.getProperty("java.io.tmpdir")</code>.</li>
039: * </ul>
040: * </p>
041: *
042: * <p>When using the <code>DiskFileItemFactory</code>, then you should
043: * consider the following: Temporary files are automatically deleted as
044: * soon as they are no longer needed. (More precisely, when the
045: * corresponding instance of {@link java.io.File} is garbage collected.)
046: * This is done by the so-called reaper thread, which is started
047: * automatically when the class {@link org.apache.commons.io.FileCleaner}
048: * is loaded. It might make sense to terminate that thread, for example,
049: * if your web application ends. See the section on "Resource cleanup"
050: * in the users guide of commons-fileupload.</p>
051: *
052: * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
053: *
054: * @since FileUpload 1.1
055: *
056: * @version $Id: DiskFileItemFactory.java 502350 2007-02-01 20:42:48Z jochen $
057: */
058: public class DiskFileItemFactory implements FileItemFactory {
059:
060: // ----------------------------------------------------- Manifest constants
061:
062: /**
063: * The default threshold above which uploads will be stored on disk.
064: */
065: public static final int DEFAULT_SIZE_THRESHOLD = 10240;
066:
067: // ----------------------------------------------------- Instance Variables
068:
069: /**
070: * The directory in which uploaded files will be stored, if stored on disk.
071: */
072: private File repository;
073:
074: /**
075: * The threshold above which uploads will be stored on disk.
076: */
077: private int sizeThreshold = DEFAULT_SIZE_THRESHOLD;
078:
079: // ----------------------------------------------------------- Constructors
080:
081: /**
082: * Constructs an unconfigured instance of this class. The resulting factory
083: * may be configured by calling the appropriate setter methods.
084: */
085: public DiskFileItemFactory() {
086: // Does nothing.
087: }
088:
089: /**
090: * Constructs a preconfigured instance of this class.
091: *
092: * @param sizeThreshold The threshold, in bytes, below which items will be
093: * retained in memory and above which they will be
094: * stored as a file.
095: * @param repository The data repository, which is the directory in
096: * which files will be created, should the item size
097: * exceed the threshold.
098: */
099: public DiskFileItemFactory(int sizeThreshold, File repository) {
100: this .sizeThreshold = sizeThreshold;
101: this .repository = repository;
102: }
103:
104: // ------------------------------------------------------------- Properties
105:
106: /**
107: * Returns the directory used to temporarily store files that are larger
108: * than the configured size threshold.
109: *
110: * @return The directory in which temporary files will be located.
111: *
112: * @see #setRepository(java.io.File)
113: *
114: */
115: public File getRepository() {
116: return repository;
117: }
118:
119: /**
120: * Sets the directory used to temporarily store files that are larger
121: * than the configured size threshold.
122: *
123: * @param repository The directory in which temporary files will be located.
124: *
125: * @see #getRepository()
126: *
127: */
128: public void setRepository(File repository) {
129: this .repository = repository;
130: }
131:
132: /**
133: * Returns the size threshold beyond which files are written directly to
134: * disk. The default value is 10240 bytes.
135: *
136: * @return The size threshold, in bytes.
137: *
138: * @see #setSizeThreshold(int)
139: */
140: public int getSizeThreshold() {
141: return sizeThreshold;
142: }
143:
144: /**
145: * Sets the size threshold beyond which files are written directly to disk.
146: *
147: * @param sizeThreshold The size threshold, in bytes.
148: *
149: * @see #getSizeThreshold()
150: *
151: */
152: public void setSizeThreshold(int sizeThreshold) {
153: this .sizeThreshold = sizeThreshold;
154: }
155:
156: // --------------------------------------------------------- Public Methods
157:
158: /**
159: * Create a new {@link org.apache.commons.fileupload.disk.DiskFileItem}
160: * instance from the supplied parameters and the local factory
161: * configuration.
162: *
163: * @param fieldName The name of the form field.
164: * @param contentType The content type of the form field.
165: * @param isFormField <code>true</code> if this is a plain form field;
166: * <code>false</code> otherwise.
167: * @param fileName The name of the uploaded file, if any, as supplied
168: * by the browser or other client.
169: *
170: * @return The newly created file item.
171: */
172: public FileItem createItem(String fieldName, String contentType,
173: boolean isFormField, String fileName) {
174: return new DiskFileItem(fieldName, contentType, isFormField,
175: fileName, sizeThreshold, repository);
176: }
177:
178: }
|