1. /*
  2. * $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 $
  3. * $Revision: 1.3 $
  4. * $Date: 2003/06/01 00:18:13 $
  5. *
  6. * ====================================================================
  7. *
  8. * The Apache Software License, Version 1.1
  9. *
  10. * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
  11. * reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in
  22. * the documentation and/or other materials provided with the
  23. * distribution.
  24. *
  25. * 3. The end-user documentation included with the redistribution, if
  26. * any, must include the following acknowlegement:
  27. * "This product includes software developed by the
  28. * Apache Software Foundation (http://www.apache.org/)."
  29. * Alternately, this acknowlegement may appear in the software itself,
  30. * if and wherever such third-party acknowlegements normally appear.
  31. *
  32. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  33. * Foundation" must not be used to endorse or promote products derived
  34. * from this software without prior written permission. For written
  35. * permission, please contact apache@apache.org.
  36. *
  37. * 5. Products derived from this software may not be called "Apache"
  38. * nor may "Apache" appear in their names without prior written
  39. * permission of the Apache Group.
  40. *
  41. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  42. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  43. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  45. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  47. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  48. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  49. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  50. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  51. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  52. * SUCH DAMAGE.
  53. * ====================================================================
  54. *
  55. * This software consists of voluntary contributions made by many
  56. * individuals on behalf of the Apache Software Foundation. For more
  57. * information on the Apache Software Foundation, please see
  58. * <http://www.apache.org/>.
  59. *
  60. */
  61. package org.apache.commons.fileupload;
  62. import java.io.File;
  63. import java.util.List;
  64. import javax.servlet.http.HttpServletRequest;
  65. /**
  66. * <p>High level API for processing file uploads.</p>
  67. *
  68. * <p>This class handles multiple files per single HTML widget, sent using
  69. * <code>multipart/mixed</code> encoding type, as specified by
  70. * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Use {@link
  71. * #parseRequest(HttpServletRequest)} to acquire a list of {@link
  72. * org.apache.commons.fileupload.FileItem}s associated with a given HTML
  73. * widget.</p>
  74. *
  75. * <p>Individual parts will be stored in temporary disk storage or in memory,
  76. * depending on their size, and will be available as {@link
  77. * org.apache.commons.fileupload.FileItem}s.</p>
  78. *
  79. * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
  80. * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
  81. * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
  82. * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
  83. * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
  84. * @author Sean C. Sullivan
  85. *
  86. * @version $Id: DiskFileUpload.java,v 1.3 2003/06/01 00:18:13 martinc Exp $
  87. */
  88. public class DiskFileUpload
  89. extends FileUploadBase
  90. {
  91. // ----------------------------------------------------------- Data members
  92. /**
  93. * The factory to use to create new form items.
  94. */
  95. private DefaultFileItemFactory fileItemFactory;
  96. // ----------------------------------------------------------- Constructors
  97. /**
  98. * Constructs an instance of this class which uses the default factory to
  99. * create <code>FileItem</code> instances.
  100. *
  101. * @see #DiskFileUpload(DefaultFileItemFactory fileItemFactory)
  102. */
  103. public DiskFileUpload()
  104. {
  105. super();
  106. this.fileItemFactory = new DefaultFileItemFactory();
  107. }
  108. /**
  109. * Constructs an instance of this class which uses the supplied factory to
  110. * create <code>FileItem</code> instances.
  111. *
  112. * @see #DiskFileUpload()
  113. */
  114. public DiskFileUpload(DefaultFileItemFactory fileItemFactory)
  115. {
  116. super();
  117. this.fileItemFactory = fileItemFactory;
  118. }
  119. // ----------------------------------------------------- Property accessors
  120. /**
  121. * Returns the factory class used when creating file items.
  122. *
  123. * @return The factory class for new file items.
  124. */
  125. public FileItemFactory getFileItemFactory()
  126. {
  127. return fileItemFactory;
  128. }
  129. /**
  130. * Sets the factory class to use when creating file items. The factory must
  131. * be an instance of <code>DefaultFileItemFactory</code> or a subclass
  132. * thereof, or else a <code>ClassCastException</code> will be thrown.
  133. *
  134. * @param factory The factory class for new file items.
  135. */
  136. public void setFileItemFactory(FileItemFactory factory)
  137. {
  138. this.fileItemFactory = (DefaultFileItemFactory) factory;
  139. }
  140. /**
  141. * Returns the size threshold beyond which files are written directly to
  142. * disk.
  143. *
  144. * @return The size threshold, in bytes.
  145. *
  146. * @see #setSizeThreshold(int)
  147. */
  148. public int getSizeThreshold()
  149. {
  150. return fileItemFactory.getSizeThreshold();
  151. }
  152. /**
  153. * Sets the size threshold beyond which files are written directly to disk.
  154. *
  155. * @param sizeThreshold The size threshold, in bytes.
  156. *
  157. * @see #getSizeThreshold()
  158. */
  159. public void setSizeThreshold(int sizeThreshold)
  160. {
  161. fileItemFactory.setSizeThreshold(sizeThreshold);
  162. }
  163. /**
  164. * Returns the location used to temporarily store files that are larger
  165. * than the configured size threshold.
  166. *
  167. * @return The path to the temporary file location.
  168. *
  169. * @see #setRepositoryPath(String)
  170. */
  171. public String getRepositoryPath()
  172. {
  173. return fileItemFactory.getRepository().getPath();
  174. }
  175. /**
  176. * Sets the location used to temporarily store files that are larger
  177. * than the configured size threshold.
  178. *
  179. * @param repositoryPath The path to the temporary file location.
  180. *
  181. * @see #getRepositoryPath()
  182. */
  183. public void setRepositoryPath(String repositoryPath)
  184. {
  185. fileItemFactory.setRepository(new File(repositoryPath));
  186. }
  187. // --------------------------------------------------------- Public methods
  188. /**
  189. * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
  190. * compliant <code>multipart/form-data</code> stream. If files are stored
  191. * on disk, the path is given by <code>getRepository()</code>.
  192. *
  193. * @param req The servlet request to be parsed. Must be non-null.
  194. * @param sizeThreshold The max size in bytes to be stored in memory.
  195. * @param sizeMax The maximum allowed upload size, in bytes.
  196. * @param path The location where the files should be stored.
  197. *
  198. * @return A list of <code>FileItem</code> instances parsed from the
  199. * request, in the order that they were transmitted.
  200. *
  201. * @exception FileUploadException if there are problems reading/parsing
  202. * the request or storing files.
  203. */
  204. public List /* FileItem */ parseRequest(HttpServletRequest req,
  205. int sizeThreshold,
  206. long sizeMax, String path)
  207. throws FileUploadException
  208. {
  209. setSizeThreshold(sizeThreshold);
  210. setSizeMax(sizeMax);
  211. setRepositoryPath(path);
  212. return parseRequest(req);
  213. }
  214. }