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