KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > http > fileupload > DefaultFileItemFactory


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
18
19 package org.apache.tomcat.util.http.fileupload;
20
21 import java.io.File JavaDoc;
22
23
24 /**
25  * <p>The default {@link org.apache.tomcat.util.http.fileupload.FileItemFactory}
26  * implementation. This implementation creates
27  * {@link org.apache.tomcat.util.http.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  * @author <a HREF="mailto:martinc@apache.org">Martin Cooper</a>
43  *
44  * @version $Id: DefaultFileItemFactory.java 467222 2006-10-24 03:17:11Z markt $
45  */

46 public class DefaultFileItemFactory implements FileItemFactory
47 {
48
49     // ----------------------------------------------------- Manifest constants
50

51
52     /**
53      * The default threshold above which uploads will be stored on disk.
54      */

55     public static final int DEFAULT_SIZE_THRESHOLD = 10240;
56
57
58     // ----------------------------------------------------- Instance Variables
59

60
61     /**
62      * The directory in which uploaded files will be stored, if stored on disk.
63      */

64     private File JavaDoc repository;
65
66
67     /**
68      * The threshold above which uploads will be stored on disk.
69      */

70     private int sizeThreshold = DEFAULT_SIZE_THRESHOLD;
71
72
73     // ----------------------------------------------------------- Constructors
74

75
76     /**
77      * Constructs an unconfigured instance of this class. The resulting factory
78      * may be configured by calling the appropriate setter methods.
79      */

80     public DefaultFileItemFactory()
81     {
82     }
83
84
85     /**
86      * Constructs a preconfigured instance of this class.
87      *
88      * @param sizeThreshold The threshold, in bytes, below which items will be
89      * retained in memory and above which they will be
90      * stored as a file.
91      * @param repository The data repository, which is the directory in
92      * which files will be created, should the item size
93      * exceed the threshold.
94      */

95     public DefaultFileItemFactory(int sizeThreshold, File JavaDoc repository)
96     {
97         this.sizeThreshold = sizeThreshold;
98         this.repository = repository;
99     }
100
101
102     // ------------------------------------------------------------- Properties
103

104
105     /**
106      * Returns the directory used to temporarily store files that are larger
107      * than the configured size threshold.
108      *
109      * @return The directory in which temporary files will be located.
110      *
111      * @see #setRepository(java.io.File)
112      *
113      */

114     public File JavaDoc getRepository()
115     {
116         return repository;
117     }
118
119
120     /**
121      * Sets the directory used to temporarily store files that are larger
122      * than the configured size threshold.
123      *
124      * @param repository The directory in which temporary files will be located.
125      *
126      * @see #getRepository()
127      *
128      */

129     public void setRepository(File JavaDoc repository)
130     {
131         this.repository = repository;
132     }
133
134
135     /**
136      * Returns the size threshold beyond which files are written directly to
137      * disk. The default value is 1024 bytes.
138      *
139      * @return The size threshold, in bytes.
140      *
141      * @see #setSizeThreshold(int)
142      */

143     public int getSizeThreshold()
144     {
145         return sizeThreshold;
146     }
147
148
149     /**
150      * Sets the size threshold beyond which files are written directly to disk.
151      *
152      * @param sizeThreshold The size threshold, in bytes.
153      *
154      * @see #getSizeThreshold()
155      *
156      */

157     public void setSizeThreshold(int sizeThreshold)
158     {
159         this.sizeThreshold = sizeThreshold;
160     }
161
162
163     // --------------------------------------------------------- Public Methods
164

165     /**
166      * Create a new {@link org.apache.tomcat.util.http.fileupload.DefaultFileItem}
167      * instance from the supplied parameters and the local factory
168      * configuration.
169      *
170      * @param fieldName The name of the form field.
171      * @param contentType The content type of the form field.
172      * @param isFormField <code>true</code> if this is a plain form field;
173      * <code>false</code> otherwise.
174      * @param fileName The name of the uploaded file, if any, as supplied
175      * by the browser or other client.
176      *
177      * @return The newly created file item.
178      */

179     public FileItem createItem(
180             String JavaDoc fieldName,
181             String JavaDoc contentType,
182             boolean isFormField,
183             String JavaDoc fileName
184             )
185     {
186         return new DefaultFileItem(fieldName, contentType,
187                 isFormField, fileName, sizeThreshold, repository);
188     }
189
190 }
191
Popular Tags