001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.fileupload;
018    
019    import java.io.File;
020    import java.util.List;
021    import javax.servlet.http.HttpServletRequest;
022    
023    /**
024     * <p>High level API for processing file uploads.</p>
025     *
026     * <p>This class handles multiple files per single HTML widget, sent using
027     * <code>multipart/mixed</code> encoding type, as specified by
028     * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>.  Use {@link
029     * #parseRequest(HttpServletRequest)} to acquire a list of {@link
030     * org.apache.commons.fileupload.FileItem}s associated with a given HTML
031     * widget.</p>
032     *
033     * <p>Individual parts will be stored in temporary disk storage or in memory,
034     * depending on their size, and will be available as {@link
035     * org.apache.commons.fileupload.FileItem}s.</p>
036     *
037     * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
038     * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
039     * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
040     * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
041     * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
042     * @author Sean C. Sullivan
043     *
044     * @version $Id: DiskFileUpload.java 479484 2006-11-27 01:06:53Z jochen $
045     *
046     * @deprecated Use <code>ServletFileUpload</code> together with
047     *             <code>DiskFileItemFactory</code> instead.
048     */
049    public class DiskFileUpload
050        extends FileUploadBase {
051    
052        // ----------------------------------------------------------- Data members
053    
054    
055        /**
056         * The factory to use to create new form items.
057         */
058        private DefaultFileItemFactory fileItemFactory;
059    
060    
061        // ----------------------------------------------------------- Constructors
062    
063    
064        /**
065         * Constructs an instance of this class which uses the default factory to
066         * create <code>FileItem</code> instances.
067         *
068         * @see #DiskFileUpload(DefaultFileItemFactory fileItemFactory)
069         *
070         * @deprecated Use <code>FileUpload</code> instead.
071         */
072        public DiskFileUpload() {
073            super();
074            this.fileItemFactory = new DefaultFileItemFactory();
075        }
076    
077    
078        /**
079         * Constructs an instance of this class which uses the supplied factory to
080         * create <code>FileItem</code> instances.
081         *
082         * @see #DiskFileUpload()
083         * @param fileItemFactory The file item factory to use.
084         *
085         * @deprecated Use <code>FileUpload</code> instead.
086         */
087        public DiskFileUpload(DefaultFileItemFactory fileItemFactory) {
088            super();
089            this.fileItemFactory = fileItemFactory;
090        }
091    
092    
093        // ----------------------------------------------------- Property accessors
094    
095    
096        /**
097         * Returns the factory class used when creating file items.
098         *
099         * @return The factory class for new file items.
100         *
101         * @deprecated Use <code>FileUpload</code> instead.
102         */
103        public FileItemFactory getFileItemFactory() {
104            return fileItemFactory;
105        }
106    
107    
108        /**
109         * Sets the factory class to use when creating file items. The factory must
110         * be an instance of <code>DefaultFileItemFactory</code> or a subclass
111         * thereof, or else a <code>ClassCastException</code> will be thrown.
112         *
113         * @param factory The factory class for new file items.
114         *
115         * @deprecated Use <code>FileUpload</code> instead.
116         */
117        public void setFileItemFactory(FileItemFactory factory) {
118            this.fileItemFactory = (DefaultFileItemFactory) factory;
119        }
120    
121    
122        /**
123         * Returns the size threshold beyond which files are written directly to
124         * disk.
125         *
126         * @return The size threshold, in bytes.
127         *
128         * @see #setSizeThreshold(int)
129         *
130         * @deprecated Use <code>DiskFileItemFactory</code> instead.
131         */
132        public int getSizeThreshold() {
133            return fileItemFactory.getSizeThreshold();
134        }
135    
136    
137        /**
138         * Sets the size threshold beyond which files are written directly to disk.
139         *
140         * @param sizeThreshold The size threshold, in bytes.
141         *
142         * @see #getSizeThreshold()
143         *
144         * @deprecated Use <code>DiskFileItemFactory</code> instead.
145         */
146        public void setSizeThreshold(int sizeThreshold) {
147            fileItemFactory.setSizeThreshold(sizeThreshold);
148        }
149    
150    
151        /**
152         * Returns the location used to temporarily store files that are larger
153         * than the configured size threshold.
154         *
155         * @return The path to the temporary file location.
156         *
157         * @see #setRepositoryPath(String)
158         *
159         * @deprecated Use <code>DiskFileItemFactory</code> instead.
160         */
161        public String getRepositoryPath() {
162            return fileItemFactory.getRepository().getPath();
163        }
164    
165    
166        /**
167         * Sets the location used to temporarily store files that are larger
168         * than the configured size threshold.
169         *
170         * @param repositoryPath The path to the temporary file location.
171         *
172         * @see #getRepositoryPath()
173         *
174         * @deprecated Use <code>DiskFileItemFactory</code> instead.
175         */
176        public void setRepositoryPath(String repositoryPath) {
177            fileItemFactory.setRepository(new File(repositoryPath));
178        }
179    
180    
181        // --------------------------------------------------------- Public methods
182    
183    
184        /**
185         * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
186         * compliant <code>multipart/form-data</code> stream. If files are stored
187         * on disk, the path is given by <code>getRepository()</code>.
188         *
189         * @param req           The servlet request to be parsed. Must be non-null.
190         * @param sizeThreshold The max size in bytes to be stored in memory.
191         * @param sizeMax       The maximum allowed upload size, in bytes.
192         * @param path          The location where the files should be stored.
193         *
194         * @return A list of <code>FileItem</code> instances parsed from the
195         *         request, in the order that they were transmitted.
196         *
197         * @throws FileUploadException if there are problems reading/parsing
198         *                             the request or storing files.
199         *
200         * @deprecated Use <code>ServletFileUpload</code> instead.
201         */
202        public List /* FileItem */ parseRequest(HttpServletRequest req,
203                                                int sizeThreshold,
204                                                long sizeMax, String path)
205            throws FileUploadException {
206            setSizeThreshold(sizeThreshold);
207            setSizeMax(sizeMax);
208            setRepositoryPath(path);
209            return parseRequest(req);
210        }
211    
212    }