KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > web > fileupload > disk > DiskFileItemFactory


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.myvietnam.mvncore.web.fileupload.disk;
17
18 import java.io.File JavaDoc;
19
20 import net.myvietnam.mvncore.web.fileupload.FileItem;
21 import net.myvietnam.mvncore.web.fileupload.FileItemFactory;
22
23 /**
24  * <p>The default {@link org.apache.commons.fileupload.FileItemFactory}
25  * implementation. This implementation creates
26  * {@link org.apache.commons.fileupload.FileItem} instances which keep their
27  * content either in memory, for smaller items, or in a temporary file on disk,
28  * for larger items. The size threshold, above which content will be stored on
29  * disk, is configurable, as is the directory in which temporary files will be
30  * created.</p>
31  *
32  * <p>If not otherwise configured, the default configuration values are as
33  * follows:
34  * <ul>
35  * <li>Size threshold is 10KB.</li>
36  * <li>Repository is the system default temp directory, as returned by
37  * <code>System.getProperty("java.io.tmpdir")</code>.</li>
38  * </ul>
39  * </p>
40  *
41  * @author <a HREF="mailto:martinc@apache.org">Martin Cooper</a>
42  *
43  * @since FileUpload 1.1
44  *
45  * @version $Id: DiskFileItemFactory.java,v 1.2 2006/02/12 04:43:11 minhnn Exp $
46  */

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

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

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

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

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

139     public int getSizeThreshold() {
140         return sizeThreshold;
141     }
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
157     // --------------------------------------------------------- Public Methods
158

159     /**
160      * Create a new {@link org.apache.commons.fileupload.disk.DiskFileItem}
161      * instance from the supplied parameters and the local factory
162      * configuration.
163      *
164      * @param fieldName The name of the form field.
165      * @param contentType The content type of the form field.
166      * @param isFormField <code>true</code> if this is a plain form field;
167      * <code>false</code> otherwise.
168      * @param fileName The name of the uploaded file, if any, as supplied
169      * by the browser or other client.
170      *
171      * @return The newly created file item.
172      */

173     public FileItem createItem(
174             String JavaDoc fieldName,
175             String JavaDoc contentType,
176             boolean isFormField,
177             String JavaDoc fileName
178             ) {
179         return new DiskFileItem(fieldName, contentType,
180                 isFormField, fileName, sizeThreshold, repository);
181     }
182
183 }
184
Popular Tags