001: /*
002: * $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 $
003: * $Revision: 1.3 $
004: * $Date: 2003/06/01 00:18:13 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Commons", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: */
061:
062: package de.ug2t.extTools.httpFileUpLoad;
063:
064: import java.io.*;
065: import java.util.*;
066:
067: import javax.servlet.http.*;
068:
069: /**
070: * <p>
071: * High level API for processing file uploads.
072: * </p>
073: *
074: * <p>
075: * This class handles multiple files per single MARKUP widget, sent using
076: * <code>multipart/mixed</code> encoding type, as specified by <a
077: * href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Use {@link
078: * #parseRequest(HttpServletRequest)} to acquire a list of {@link
079: * org.apache.commons.fileupload.FileItem}s associated with a given MARKUP
080: * widget.
081: * </p>
082: *
083: * <p>
084: * Individual parts will be stored in temporary disk storage or in memory,
085: * depending on their size, and will be available as {@link
086: * org.apache.commons.fileupload.FileItem}s.
087: * </p>
088: *
089: * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
090: * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
091: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
092: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
093: * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
094: * @author Sean C. Sullivan
095: *
096: * @version $Id: DiskFileUpload.java,v 1.3 2003/06/01 00:18:13 martinc Exp $
097: */
098: public class DiskFileUpload extends AFileUploadBase {
099:
100: // ----------------------------------------------------------- Data members
101:
102: /**
103: * The factory to use to create new form items.
104: */
105: private DefaultFileItemFactory fileItemFactory;
106:
107: // ----------------------------------------------------------- Constructors
108:
109: /**
110: * Constructs an instance of this class which uses the default factory to
111: * create <code>FileItem</code> instances.
112: *
113: * @see #DiskFileUpload(DefaultFileItemFactory fileItemFactory)
114: */
115: public DiskFileUpload() {
116: super ();
117: this .fileItemFactory = new DefaultFileItemFactory();
118: }
119:
120: /**
121: * Constructs an instance of this class which uses the supplied factory to
122: * create <code>FileItem</code> instances.
123: *
124: * @see #DiskFileUpload()
125: */
126: public DiskFileUpload(DefaultFileItemFactory fileItemFactory) {
127: super ();
128: this .fileItemFactory = fileItemFactory;
129: }
130:
131: // ----------------------------------------------------- Property accessors
132:
133: /**
134: * Returns the factory class used when creating file items.
135: *
136: * @return The factory class for new file items.
137: */
138: public FileItemFactory getFileItemFactory() {
139: return fileItemFactory;
140: }
141:
142: /**
143: * Sets the factory class to use when creating file items. The factory must be
144: * an instance of <code>DefaultFileItemFactory</code> or a subclass thereof,
145: * or else a <code>ClassCastException</code> will be thrown.
146: *
147: * @param factory
148: * The factory class for new file items.
149: */
150: public void setFileItemFactory(FileItemFactory factory) {
151: this .fileItemFactory = (DefaultFileItemFactory) factory;
152: }
153:
154: /**
155: * Returns the size threshold beyond which files are written directly to disk.
156: *
157: * @return The size threshold, in bytes.
158: *
159: * @see #setSizeThreshold(int)
160: */
161: public int getSizeThreshold() {
162: return fileItemFactory.getSizeThreshold();
163: }
164:
165: /**
166: * Sets the size threshold beyond which files are written directly to disk.
167: *
168: * @param sizeThreshold
169: * The size threshold, in bytes.
170: *
171: * @see #getSizeThreshold()
172: */
173: public void setSizeThreshold(int sizeThreshold) {
174: fileItemFactory.setSizeThreshold(sizeThreshold);
175: }
176:
177: /**
178: * Returns the location used to temporarily store files that are larger than
179: * the configured size threshold.
180: *
181: * @return The path to the temporary file location.
182: *
183: * @see #setRepositoryPath(String)
184: */
185: public String getRepositoryPath() {
186: return fileItemFactory.getRepository().getPath();
187: }
188:
189: /**
190: * Sets the location used to temporarily store files that are larger than the
191: * configured size threshold.
192: *
193: * @param repositoryPath
194: * The path to the temporary file location.
195: *
196: * @see #getRepositoryPath()
197: */
198: public void setRepositoryPath(String repositoryPath) {
199: fileItemFactory.setRepository(new File(repositoryPath));
200: }
201:
202: // --------------------------------------------------------- Public methods
203:
204: /**
205: * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
206: * compliant <code>multipart/form-data</code> stream. If files are stored on
207: * disk, the path is given by <code>getRepository()</code>.
208: *
209: * @param req
210: * The servlet request to be parsed. Must be non-null.
211: * @param sizeThreshold
212: * The max size in bytes to be stored in memory.
213: * @param sizeMax
214: * The maximum allowed upload size, in bytes.
215: * @param path
216: * The location where the files should be stored.
217: *
218: * @return A list of <code>FileItem</code> instances parsed from the
219: * request, in the order that they were transmitted.
220: *
221: * @exception FileUploadException
222: * if there are problems reading/parsing the request or storing
223: * files.
224: */
225: public List /* FileItem */parseRequest(HttpServletRequest req,
226: int sizeThreshold, long sizeMax, String path)
227: throws FileUploadException {
228: setSizeThreshold(sizeThreshold);
229: setSizeMax(sizeMax);
230: setRepositoryPath(path);
231: return parseRequest(req);
232: }
233:
234: }
|