1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 package org.apache.commons.fileupload;
64
65
66 import java.io.File;
67 import java.util.List;
68
69 import javax.portlet.ActionRequest;
70
71
72 /***
73 * <p>High level API for processing file uploads.</p>
74 *
75 * <p>This class handles multiple files per single HTML widget, sent using
76 * <code>multipart/mixed</code> encoding type, as specified by
77 * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Use {@link
78 * #parseRequest(HttpServletRequest)} to acquire a list of {@link
79 * org.apache.commons.fileupload.FileItem}s associated with a given HTML
80 * widget.</p>
81 *
82 * <p>Individual parts will be stored in temporary disk storage or in memory,
83 * depending on their size, and will be available as {@link
84 * org.apache.commons.fileupload.FileItem}s.</p>
85 *
86 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
87 * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
88 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
89 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
90 * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
91 * @author Sean C. Sullivan
92 *
93 * @version $Id: PortletDiskFileUpload.java,v 1.1 2003/10/01 22:21:43 jsackett Exp $
94 */
95 public class PortletDiskFileUpload
96 extends PortletFileUploadBase
97 {
98
99
100
101
102 /***
103 * The factory to use to create new form items.
104 */
105 private DefaultFileItemFactory fileItemFactory;
106
107
108
109
110
111 /***
112 * Constructs an instance of this class which uses the default factory to
113 * create <code>FileItem</code> instances.
114 *
115 * @see #DiskFileUpload(DefaultFileItemFactory fileItemFactory)
116 */
117 public PortletDiskFileUpload()
118 {
119 super();
120 this.fileItemFactory = new DefaultFileItemFactory();
121 }
122
123
124 /***
125 * Constructs an instance of this class which uses the supplied factory to
126 * create <code>FileItem</code> instances.
127 *
128 * @see #DiskFileUpload()
129 */
130 public PortletDiskFileUpload(DefaultFileItemFactory fileItemFactory)
131 {
132 super();
133 this.fileItemFactory = fileItemFactory;
134 }
135
136
137
138
139
140 /***
141 * Returns the factory class used when creating file items.
142 *
143 * @return The factory class for new file items.
144 */
145 public FileItemFactory getFileItemFactory()
146 {
147 return fileItemFactory;
148 }
149
150
151 /***
152 * Sets the factory class to use when creating file items. The factory must
153 * be an instance of <code>DefaultFileItemFactory</code> or a subclass
154 * thereof, or else a <code>ClassCastException</code> will be thrown.
155 *
156 * @param factory The factory class for new file items.
157 */
158 public void setFileItemFactory(FileItemFactory factory)
159 {
160 this.fileItemFactory = (DefaultFileItemFactory) factory;
161 }
162
163
164 /***
165 * Returns the size threshold beyond which files are written directly to
166 * disk.
167 *
168 * @return The size threshold, in bytes.
169 *
170 * @see #setSizeThreshold(int)
171 */
172 public int getSizeThreshold()
173 {
174 return fileItemFactory.getSizeThreshold();
175 }
176
177
178 /***
179 * Sets the size threshold beyond which files are written directly to disk.
180 *
181 * @param sizeThreshold The size threshold, in bytes.
182 *
183 * @see #getSizeThreshold()
184 */
185 public void setSizeThreshold(int sizeThreshold)
186 {
187 fileItemFactory.setSizeThreshold(sizeThreshold);
188 }
189
190
191 /***
192 * Returns the location used to temporarily store files that are larger
193 * than the configured size threshold.
194 *
195 * @return The path to the temporary file location.
196 *
197 * @see #setRepositoryPath(String)
198 */
199 public String getRepositoryPath()
200 {
201 return fileItemFactory.getRepository().getPath();
202 }
203
204
205 /***
206 * Sets the location used to temporarily store files that are larger
207 * than the configured size threshold.
208 *
209 * @param repositoryPath The path to the temporary file location.
210 *
211 * @see #getRepositoryPath()
212 */
213 public void setRepositoryPath(String repositoryPath)
214 {
215 fileItemFactory.setRepository(new File(repositoryPath));
216 }
217
218
219
220
221
222 /***
223 * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
224 * compliant <code>multipart/form-data</code> stream. If files are stored
225 * on disk, the path is given by <code>getRepository()</code>.
226 *
227 * @param req The servlet request to be parsed. Must be non-null.
228 * @param sizeThreshold The max size in bytes to be stored in memory.
229 * @param sizeMax The maximum allowed upload size, in bytes.
230 * @param path The location where the files should be stored.
231 *
232 * @return A list of <code>FileItem</code> instances parsed from the
233 * request, in the order that they were transmitted.
234 *
235 * @exception FileUploadException if there are problems reading/parsing
236 * the request or storing files.
237 */
238 public List
239 int sizeThreshold,
240 long sizeMax, String path)
241 throws FileUploadException
242 {
243 setSizeThreshold(sizeThreshold);
244 setSizeMax(sizeMax);
245 setRepositoryPath(path);
246 return parseRequest(req);
247 }
248
249 }