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:
018: package org.apache.tomcat.util.http.fileupload;
019:
020: import java.io.File;
021: import java.util.List;
022: import javax.servlet.http.HttpServletRequest;
023:
024: /**
025: * <p>High level API for processing file uploads.</p>
026: *
027: * <p>This class handles multiple files per single HTML widget, sent using
028: * <code>multipart/mixed</code> encoding type, as specified by
029: * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Use {@link
030: * #parseRequest(HttpServletRequest)} to acquire a list of {@link
031: * org.apache.tomcat.util.http.fileupload.FileItem}s associated with a given HTML
032: * widget.</p>
033: *
034: * <p>Individual parts will be stored in temporary disk storage or in memory,
035: * depending on their size, and will be available as {@link
036: * org.apache.tomcat.util.http.fileupload.FileItem}s.</p>
037: *
038: * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
039: * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
040: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
041: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
042: * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
043: * @author Sean C. Sullivan
044: *
045: * @version $Id: DiskFileUpload.java 467222 2006-10-24 03:17:11Z markt $
046: */
047: public class DiskFileUpload extends FileUploadBase {
048:
049: // ----------------------------------------------------------- Data members
050:
051: /**
052: * The factory to use to create new form items.
053: */
054: private DefaultFileItemFactory fileItemFactory;
055:
056: // ----------------------------------------------------------- Constructors
057:
058: /**
059: * Constructs an instance of this class which uses the default factory to
060: * create <code>FileItem</code> instances.
061: *
062: * @see #DiskFileUpload(DefaultFileItemFactory fileItemFactory)
063: */
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: */
075: public DiskFileUpload(DefaultFileItemFactory fileItemFactory) {
076: super ();
077: this .fileItemFactory = fileItemFactory;
078: }
079:
080: // ----------------------------------------------------- Property accessors
081:
082: /**
083: * Returns the factory class used when creating file items.
084: *
085: * @return The factory class for new file items.
086: */
087: public FileItemFactory getFileItemFactory() {
088: return fileItemFactory;
089: }
090:
091: /**
092: * Sets the factory class to use when creating file items. The factory must
093: * be an instance of <code>DefaultFileItemFactory</code> or a subclass
094: * thereof, or else a <code>ClassCastException</code> will be thrown.
095: *
096: * @param factory The factory class for new file items.
097: */
098: public void setFileItemFactory(FileItemFactory factory) {
099: this .fileItemFactory = (DefaultFileItemFactory) factory;
100: }
101:
102: /**
103: * Returns the size threshold beyond which files are written directly to
104: * disk.
105: *
106: * @return The size threshold, in bytes.
107: *
108: * @see #setSizeThreshold(int)
109: */
110: public int getSizeThreshold() {
111: return fileItemFactory.getSizeThreshold();
112: }
113:
114: /**
115: * Sets the size threshold beyond which files are written directly to disk.
116: *
117: * @param sizeThreshold The size threshold, in bytes.
118: *
119: * @see #getSizeThreshold()
120: */
121: public void setSizeThreshold(int sizeThreshold) {
122: fileItemFactory.setSizeThreshold(sizeThreshold);
123: }
124:
125: /**
126: * Returns the location used to temporarily store files that are larger
127: * than the configured size threshold.
128: *
129: * @return The path to the temporary file location.
130: *
131: * @see #setRepositoryPath(String)
132: */
133: public String getRepositoryPath() {
134: return fileItemFactory.getRepository().getPath();
135: }
136:
137: /**
138: * Sets the location used to temporarily store files that are larger
139: * than the configured size threshold.
140: *
141: * @param repositoryPath The path to the temporary file location.
142: *
143: * @see #getRepositoryPath()
144: */
145: public void setRepositoryPath(String repositoryPath) {
146: fileItemFactory.setRepository(new File(repositoryPath));
147: }
148:
149: // --------------------------------------------------------- Public methods
150:
151: /**
152: * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
153: * compliant <code>multipart/form-data</code> stream. If files are stored
154: * on disk, the path is given by <code>getRepository()</code>.
155: *
156: * @param req The servlet request to be parsed. Must be non-null.
157: * @param sizeThreshold The max size in bytes to be stored in memory.
158: * @param sizeMax The maximum allowed upload size, in bytes.
159: * @param path The location where the files should be stored.
160: *
161: * @return A list of <code>FileItem</code> instances parsed from the
162: * request, in the order that they were transmitted.
163: *
164: * @exception FileUploadException if there are problems reading/parsing
165: * the request or storing files.
166: */
167: public List /* FileItem */parseRequest(HttpServletRequest req,
168: int sizeThreshold, long sizeMax, String path)
169: throws FileUploadException {
170: setSizeThreshold(sizeThreshold);
171: setSizeMax(sizeMax);
172: setRepositoryPath(path);
173: return parseRequest(req);
174: }
175:
176: }
|