KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.io.File JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26
27 /**
28  * <p>High level API for processing file uploads.</p>
29  *
30  * <p>This class handles multiple files per single HTML widget, sent using
31  * <code>multipart/mixed</code> encoding type, as specified by
32  * <a HREF="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Use {@link
33  * #parseRequest(HttpServletRequest)} to acquire a list of {@link
34  * org.apache.tomcat.util.http.fileupload.FileItem}s associated with a given HTML
35  * widget.</p>
36  *
37  * <p>Individual parts will be stored in temporary disk storage or in memory,
38  * depending on their size, and will be available as {@link
39  * org.apache.tomcat.util.http.fileupload.FileItem}s.</p>
40  *
41  * @author <a HREF="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
42  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
43  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
44  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
45  * @author <a HREF="mailto:martinc@apache.org">Martin Cooper</a>
46  * @author Sean C. Sullivan
47  *
48  * @version $Id: DiskFileUpload.java 467222 2006-10-24 03:17:11Z markt $
49  */

50 public class DiskFileUpload
51     extends FileUploadBase
52  {
53
54     // ----------------------------------------------------------- Data members
55

56
57     /**
58      * The factory to use to create new form items.
59      */

60     private DefaultFileItemFactory fileItemFactory;
61
62
63     // ----------------------------------------------------------- Constructors
64

65
66     /**
67      * Constructs an instance of this class which uses the default factory to
68      * create <code>FileItem</code> instances.
69      *
70      * @see #DiskFileUpload(DefaultFileItemFactory fileItemFactory)
71      */

72     public DiskFileUpload()
73     {
74         super();
75         this.fileItemFactory = new DefaultFileItemFactory();
76     }
77
78
79     /**
80      * Constructs an instance of this class which uses the supplied factory to
81      * create <code>FileItem</code> instances.
82      *
83      * @see #DiskFileUpload()
84      */

85     public DiskFileUpload(DefaultFileItemFactory fileItemFactory)
86     {
87         super();
88         this.fileItemFactory = fileItemFactory;
89     }
90
91
92     // ----------------------------------------------------- Property accessors
93

94
95     /**
96      * Returns the factory class used when creating file items.
97      *
98      * @return The factory class for new file items.
99      */

100     public FileItemFactory getFileItemFactory()
101     {
102         return fileItemFactory;
103     }
104
105
106     /**
107      * Sets the factory class to use when creating file items. The factory must
108      * be an instance of <code>DefaultFileItemFactory</code> or a subclass
109      * thereof, or else a <code>ClassCastException</code> will be thrown.
110      *
111      * @param factory The factory class for new file items.
112      */

113     public void setFileItemFactory(FileItemFactory factory)
114     {
115         this.fileItemFactory = (DefaultFileItemFactory) factory;
116     }
117
118
119     /**
120      * Returns the size threshold beyond which files are written directly to
121      * disk.
122      *
123      * @return The size threshold, in bytes.
124      *
125      * @see #setSizeThreshold(int)
126      */

127     public int getSizeThreshold()
128     {
129         return fileItemFactory.getSizeThreshold();
130     }
131
132
133     /**
134      * Sets the size threshold beyond which files are written directly to disk.
135      *
136      * @param sizeThreshold The size threshold, in bytes.
137      *
138      * @see #getSizeThreshold()
139      */

140     public void setSizeThreshold(int sizeThreshold)
141     {
142         fileItemFactory.setSizeThreshold(sizeThreshold);
143     }
144
145
146     /**
147      * Returns the location used to temporarily store files that are larger
148      * than the configured size threshold.
149      *
150      * @return The path to the temporary file location.
151      *
152      * @see #setRepositoryPath(String)
153      */

154     public String JavaDoc getRepositoryPath()
155     {
156         return fileItemFactory.getRepository().getPath();
157     }
158
159
160     /**
161      * Sets the location used to temporarily store files that are larger
162      * than the configured size threshold.
163      *
164      * @param repositoryPath The path to the temporary file location.
165      *
166      * @see #getRepositoryPath()
167      */

168     public void setRepositoryPath(String JavaDoc repositoryPath)
169     {
170         fileItemFactory.setRepository(new File JavaDoc(repositoryPath));
171     }
172
173
174     // --------------------------------------------------------- Public methods
175

176
177     /**
178      * Processes an <a HREF="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
179      * compliant <code>multipart/form-data</code> stream. If files are stored
180      * on disk, the path is given by <code>getRepository()</code>.
181      *
182      * @param req The servlet request to be parsed. Must be non-null.
183      * @param sizeThreshold The max size in bytes to be stored in memory.
184      * @param sizeMax The maximum allowed upload size, in bytes.
185      * @param path The location where the files should be stored.
186      *
187      * @return A list of <code>FileItem</code> instances parsed from the
188      * request, in the order that they were transmitted.
189      *
190      * @exception FileUploadException if there are problems reading/parsing
191      * the request or storing files.
192      */

193     public List JavaDoc /* FileItem */ parseRequest(HttpServletRequest JavaDoc req,
194                                             int sizeThreshold,
195                                             long sizeMax, String JavaDoc path)
196         throws FileUploadException
197     {
198         setSizeThreshold(sizeThreshold);
199         setSizeMax(sizeMax);
200         setRepositoryPath(path);
201         return parseRequest(req);
202     }
203
204 }
205
Popular Tags