KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/DiskFileUpload.java,v 1.3 2003/06/01 00:18:13 martinc Exp $
3  * $Revision: 1.3 $
4  * $Date: 2003/06/01 00:18:13 $
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
66 import java.io.File JavaDoc;
67 import java.util.List JavaDoc;
68 import javax.servlet.http.HttpServletRequest JavaDoc;
69
70
71 /**
72  * <p>High level API for processing file uploads.</p>
73  *
74  * <p>This class handles multiple files per single HTML widget, sent using
75  * <code>multipart/mixed</code> encoding type, as specified by
76  * <a HREF="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Use {@link
77  * #parseRequest(HttpServletRequest)} to acquire a list of {@link
78  * org.apache.commons.fileupload.FileItem}s associated with a given HTML
79  * widget.</p>
80  *
81  * <p>Individual parts will be stored in temporary disk storage or in memory,
82  * depending on their size, and will be available as {@link
83  * org.apache.commons.fileupload.FileItem}s.</p>
84  *
85  * @author <a HREF="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
86  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
87  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
88  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
89  * @author <a HREF="mailto:martinc@apache.org">Martin Cooper</a>
90  * @author Sean C. Sullivan
91  *
92  * @version $Id: DiskFileUpload.java,v 1.3 2003/06/01 00:18:13 martinc Exp $
93  */

94 public class DiskFileUpload
95     extends FileUploadBase
96  {
97
98     // ----------------------------------------------------------- Data members
99

100
101     /**
102      * The factory to use to create new form items.
103      */

104     private DefaultFileItemFactory fileItemFactory;
105
106
107     // ----------------------------------------------------------- Constructors
108

109
110     /**
111      * Constructs an instance of this class which uses the default factory to
112      * create <code>FileItem</code> instances.
113      *
114      * @see #DiskFileUpload(DefaultFileItemFactory fileItemFactory)
115      */

116     public DiskFileUpload()
117     {
118         super();
119         this.fileItemFactory = new DefaultFileItemFactory();
120     }
121
122
123     /**
124      * Constructs an instance of this class which uses the supplied factory to
125      * create <code>FileItem</code> instances.
126      *
127      * @see #DiskFileUpload()
128      */

129     public DiskFileUpload(DefaultFileItemFactory fileItemFactory)
130     {
131         super();
132         this.fileItemFactory = fileItemFactory;
133     }
134
135
136     // ----------------------------------------------------- Property accessors
137

138
139     /**
140      * Returns the factory class used when creating file items.
141      *
142      * @return The factory class for new file items.
143      */

144     public FileItemFactory getFileItemFactory()
145     {
146         return fileItemFactory;
147     }
148
149
150     /**
151      * Sets the factory class to use when creating file items. The factory must
152      * be an instance of <code>DefaultFileItemFactory</code> or a subclass
153      * thereof, or else a <code>ClassCastException</code> will be thrown.
154      *
155      * @param factory The factory class for new file items.
156      */

157     public void setFileItemFactory(FileItemFactory factory)
158     {
159         this.fileItemFactory = (DefaultFileItemFactory) factory;
160     }
161
162
163     /**
164      * Returns the size threshold beyond which files are written directly to
165      * disk.
166      *
167      * @return The size threshold, in bytes.
168      *
169      * @see #setSizeThreshold(int)
170      */

171     public int getSizeThreshold()
172     {
173         return fileItemFactory.getSizeThreshold();
174     }
175
176
177     /**
178      * Sets the size threshold beyond which files are written directly to disk.
179      *
180      * @param sizeThreshold The size threshold, in bytes.
181      *
182      * @see #getSizeThreshold()
183      */

184     public void setSizeThreshold(int sizeThreshold)
185     {
186         fileItemFactory.setSizeThreshold(sizeThreshold);
187     }
188
189
190     /**
191      * Returns the location used to temporarily store files that are larger
192      * than the configured size threshold.
193      *
194      * @return The path to the temporary file location.
195      *
196      * @see #setRepositoryPath(String)
197      */

198     public String JavaDoc getRepositoryPath()
199     {
200         return fileItemFactory.getRepository().getPath();
201     }
202
203
204     /**
205      * Sets the location used to temporarily store files that are larger
206      * than the configured size threshold.
207      *
208      * @param repositoryPath The path to the temporary file location.
209      *
210      * @see #getRepositoryPath()
211      */

212     public void setRepositoryPath(String JavaDoc repositoryPath)
213     {
214         fileItemFactory.setRepository(new File JavaDoc(repositoryPath));
215     }
216
217
218     // --------------------------------------------------------- Public methods
219

220
221     /**
222      * Processes an <a HREF="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
223      * compliant <code>multipart/form-data</code> stream. If files are stored
224      * on disk, the path is given by <code>getRepository()</code>.
225      *
226      * @param req The servlet request to be parsed. Must be non-null.
227      * @param sizeThreshold The max size in bytes to be stored in memory.
228      * @param sizeMax The maximum allowed upload size, in bytes.
229      * @param path The location where the files should be stored.
230      *
231      * @return A list of <code>FileItem</code> instances parsed from the
232      * request, in the order that they were transmitted.
233      *
234      * @exception FileUploadException if there are problems reading/parsing
235      * the request or storing files.
236      */

237     public List JavaDoc /* FileItem */ parseRequest(HttpServletRequest JavaDoc req,
238                                             int sizeThreshold,
239                                             long sizeMax, String JavaDoc path)
240         throws FileUploadException
241     {
242         setSizeThreshold(sizeThreshold);
243         setSizeMax(sizeMax);
244         setRepositoryPath(path);
245         return parseRequest(req);
246     }
247
248 }
249
Popular Tags