KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > fileupload > DefaultFileItemFactory


1 /*
2  * $Header: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/DefaultFileItemFactory.java,v 1.2 2003/05/31 22:31:08 martinc Exp $
3  * $Revision: 1.2 $
4  * $Date: 2003/05/31 22:31:08 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61
62
63 package org.apache.commons.fileupload;
64
65 import java.io.File JavaDoc;
66
67
68 /**
69  * <p>The default {@link org.apache.commons.fileupload.FileItemFactory}
70  * implementation. This implementation creates
71  * {@link org.apache.commons.fileupload.FileItem} instances which keep their
72  * content either in memory, for smaller items, or in a temporary file on disk,
73  * for larger items. The size threshold, above which content will be stored on
74  * disk, is configurable, as is the directory in which temporary files will be
75  * created.</p>
76  *
77  * <p>If not otherwise configured, the default configuration values are as
78  * follows:
79  * <ul>
80  * <li>Size threshold is 10KB.</li>
81  * <li>Repository is the system default temp directory, as returned by
82  * <code>System.getProperty("java.io.tmpdir")</code>.</li>
83  * </ul>
84  * </p>
85  *
86  * @author <a HREF="mailto:martinc@apache.org">Martin Cooper</a>
87  *
88  * @version $Id: DefaultFileItemFactory.java,v 1.2 2003/05/31 22:31:08 martinc Exp $
89  */

90 public class DefaultFileItemFactory implements FileItemFactory
91 {
92
93     // ----------------------------------------------------- Manifest constants
94

95
96     /**
97      * The default threshold above which uploads will be stored on disk.
98      */

99     public static final int DEFAULT_SIZE_THRESHOLD = 10240;
100
101
102     // ----------------------------------------------------- Instance Variables
103

104
105     /**
106      * The directory in which uploaded files will be stored, if stored on disk.
107      */

108     private File JavaDoc repository;
109
110
111     /**
112      * The threshold above which uploads will be stored on disk.
113      */

114     private int sizeThreshold = DEFAULT_SIZE_THRESHOLD;
115
116
117     // ----------------------------------------------------------- Constructors
118

119
120     /**
121      * Constructs an unconfigured instance of this class. The resulting factory
122      * may be configured by calling the appropriate setter methods.
123      */

124     public DefaultFileItemFactory()
125     {
126     }
127
128
129     /**
130      * Constructs a preconfigured instance of this class.
131      *
132      * @param sizeThreshold The threshold, in bytes, below which items will be
133      * retained in memory and above which they will be
134      * stored as a file.
135      * @param repository The data repository, which is the directory in
136      * which files will be created, should the item size
137      * exceed the threshold.
138      */

139     public DefaultFileItemFactory(int sizeThreshold, File JavaDoc repository)
140     {
141         this.sizeThreshold = sizeThreshold;
142         this.repository = repository;
143     }
144
145
146     // ------------------------------------------------------------- Properties
147

148
149     /**
150      * Returns the directory used to temporarily store files that are larger
151      * than the configured size threshold.
152      *
153      * @return The directory in which temporary files will be located.
154      *
155      * @see #setRepository(java.io.File)
156      *
157      */

158     public File JavaDoc getRepository()
159     {
160         return repository;
161     }
162
163
164     /**
165      * Sets the directory used to temporarily store files that are larger
166      * than the configured size threshold.
167      *
168      * @param repository The directory in which temporary files will be located.
169      *
170      * @see #getRepository()
171      *
172      */

173     public void setRepository(File JavaDoc repository)
174     {
175         this.repository = repository;
176     }
177
178
179     /**
180      * Returns the size threshold beyond which files are written directly to
181      * disk. The default value is 1024 bytes.
182      *
183      * @return The size threshold, in bytes.
184      *
185      * @see #setSizeThreshold(int)
186      */

187     public int getSizeThreshold()
188     {
189         return sizeThreshold;
190     }
191
192
193     /**
194      * Sets the size threshold beyond which files are written directly to disk.
195      *
196      * @param sizeThreshold The size threshold, in bytes.
197      *
198      * @see #getSizeThreshold()
199      *
200      */

201     public void setSizeThreshold(int sizeThreshold)
202     {
203         this.sizeThreshold = sizeThreshold;
204     }
205
206
207     // --------------------------------------------------------- Public Methods
208

209     /**
210      * Create a new {@link org.apache.commons.fileupload.DefaultFileItem}
211      * instance from the supplied parameters and the local factory
212      * configuration.
213      *
214      * @param fieldName The name of the form field.
215      * @param contentType The content type of the form field.
216      * @param isFormField <code>true</code> if this is a plain form field;
217      * <code>false</code> otherwise.
218      * @param fileName The name of the uploaded file, if any, as supplied
219      * by the browser or other client.
220      *
221      * @return The newly created file item.
222      */

223     public FileItem createItem(
224             String JavaDoc fieldName,
225             String JavaDoc contentType,
226             boolean isFormField,
227             String JavaDoc fileName
228             )
229     {
230         return new DefaultFileItem(fieldName, contentType,
231                 isFormField, fileName, sizeThreshold, repository);
232     }
233
234 }
235
Popular Tags