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 * @version $Id: DiskFileUpload.java 1454690 2013-03-09 12:08:48Z simonetripodi $
038 *
039 * @deprecated 1.1 Use <code>ServletFileUpload</code> together with
040 * <code>DiskFileItemFactory</code> instead.
041 */
042 @Deprecated
043 public class DiskFileUpload
044 extends FileUploadBase {
045
046 // ----------------------------------------------------------- Data members
047
048 /**
049 * The factory to use to create new form items.
050 */
051 private DefaultFileItemFactory fileItemFactory;
052
053 // ----------------------------------------------------------- Constructors
054
055 /**
056 * Constructs an instance of this class which uses the default factory to
057 * create <code>FileItem</code> instances.
058 *
059 * @see #DiskFileUpload(DefaultFileItemFactory fileItemFactory)
060 *
061 * @deprecated 1.1 Use <code>FileUpload</code> instead.
062 */
063 @Deprecated
064 public DiskFileUpload() {
065 super();
066 this.fileItemFactory = new DefaultFileItemFactory();
067 }
068
069 /**
070 * Constructs an instance of this class which uses the supplied factory to
071 * create <code>FileItem</code> instances.
072 *
073 * @see #DiskFileUpload()
074 * @param fileItemFactory The file item factory to use.
075 *
076 * @deprecated 1.1 Use <code>FileUpload</code> instead.
077 */
078 @Deprecated
079 public DiskFileUpload(DefaultFileItemFactory fileItemFactory) {
080 super();
081 this.fileItemFactory = fileItemFactory;
082 }
083
084 // ----------------------------------------------------- Property accessors
085
086 /**
087 * Returns the factory class used when creating file items.
088 *
089 * @return The factory class for new file items.
090 *
091 * @deprecated 1.1 Use <code>FileUpload</code> instead.
092 */
093 @Override
094 @Deprecated
095 public FileItemFactory getFileItemFactory() {
096 return fileItemFactory;
097 }
098
099 /**
100 * Sets the factory class to use when creating file items. The factory must
101 * be an instance of <code>DefaultFileItemFactory</code> or a subclass
102 * thereof, or else a <code>ClassCastException</code> will be thrown.
103 *
104 * @param factory The factory class for new file items.
105 *
106 * @deprecated 1.1 Use <code>FileUpload</code> instead.
107 */
108 @Override
109 @Deprecated
110 public void setFileItemFactory(FileItemFactory factory) {
111 this.fileItemFactory = (DefaultFileItemFactory) factory;
112 }
113
114 /**
115 * Returns the size threshold beyond which files are written directly to
116 * disk.
117 *
118 * @return The size threshold, in bytes.
119 *
120 * @see #setSizeThreshold(int)
121 *
122 * @deprecated 1.1 Use <code>DiskFileItemFactory</code> instead.
123 */
124 @Deprecated
125 public int getSizeThreshold() {
126 return fileItemFactory.getSizeThreshold();
127 }
128
129 /**
130 * Sets the size threshold beyond which files are written directly to disk.
131 *
132 * @param sizeThreshold The size threshold, in bytes.
133 *
134 * @see #getSizeThreshold()
135 *
136 * @deprecated 1.1 Use <code>DiskFileItemFactory</code> instead.
137 */
138 @Deprecated
139 public void setSizeThreshold(int sizeThreshold) {
140 fileItemFactory.setSizeThreshold(sizeThreshold);
141 }
142
143 /**
144 * Returns the location used to temporarily store files that are larger
145 * than the configured size threshold.
146 *
147 * @return The path to the temporary file location.
148 *
149 * @see #setRepositoryPath(String)
150 *
151 * @deprecated 1.1 Use <code>DiskFileItemFactory</code> instead.
152 */
153 @Deprecated
154 public String getRepositoryPath() {
155 return fileItemFactory.getRepository().getPath();
156 }
157
158 /**
159 * Sets the location used to temporarily store files that are larger
160 * than the configured size threshold.
161 *
162 * @param repositoryPath The path to the temporary file location.
163 *
164 * @see #getRepositoryPath()
165 *
166 * @deprecated 1.1 Use <code>DiskFileItemFactory</code> instead.
167 */
168 @Deprecated
169 public void setRepositoryPath(String repositoryPath) {
170 fileItemFactory.setRepository(new File(repositoryPath));
171 }
172
173 // --------------------------------------------------------- Public methods
174
175 /**
176 * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
177 * compliant <code>multipart/form-data</code> stream. If files are stored
178 * on disk, the path is given by <code>getRepository()</code>.
179 *
180 * @param req The servlet request to be parsed. Must be non-null.
181 * @param sizeThreshold The max size in bytes to be stored in memory.
182 * @param sizeMax The maximum allowed upload size, in bytes.
183 * @param path The location where the files should be stored.
184 *
185 * @return A list of <code>FileItem</code> instances parsed from the
186 * request, in the order that they were transmitted.
187 *
188 * @throws FileUploadException if there are problems reading/parsing
189 * the request or storing files.
190 *
191 * @deprecated 1.1 Use <code>ServletFileUpload</code> instead.
192 */
193 @Deprecated
194 public List<FileItem> parseRequest(HttpServletRequest req,
195 int sizeThreshold,
196 long sizeMax, String path)
197 throws FileUploadException {
198 setSizeThreshold(sizeThreshold);
199 setSizeMax(sizeMax);
200 setRepositoryPath(path);
201 return parseRequest(req);
202 }
203
204 }