1. /*
  2. * $Header: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/DefaultFileItemFactory.java,v 1.2 2003/05/31 22:31:08 martinc Exp $
  3. * $Revision: 1.2 $
  4. * $Date: 2003/05/31 22:31:08 $
  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. /**
  64. * <p>The default {@link org.apache.commons.fileupload.FileItemFactory}
  65. * implementation. This implementation creates
  66. * {@link org.apache.commons.fileupload.FileItem} instances which keep their
  67. * content either in memory, for smaller items, or in a temporary file on disk,
  68. * for larger items. The size threshold, above which content will be stored on
  69. * disk, is configurable, as is the directory in which temporary files will be
  70. * created.</p>
  71. *
  72. * <p>If not otherwise configured, the default configuration values are as
  73. * follows:
  74. * <ul>
  75. * <li>Size threshold is 10KB.</li>
  76. * <li>Repository is the system default temp directory, as returned by
  77. * <code>System.getProperty("java.io.tmpdir")</code>.</li>
  78. * </ul>
  79. * </p>
  80. *
  81. * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
  82. *
  83. * @version $Id: DefaultFileItemFactory.java,v 1.2 2003/05/31 22:31:08 martinc Exp $
  84. */
  85. public class DefaultFileItemFactory implements FileItemFactory
  86. {
  87. // ----------------------------------------------------- Manifest constants
  88. /**
  89. * The default threshold above which uploads will be stored on disk.
  90. */
  91. public static final int DEFAULT_SIZE_THRESHOLD = 10240;
  92. // ----------------------------------------------------- Instance Variables
  93. /**
  94. * The directory in which uploaded files will be stored, if stored on disk.
  95. */
  96. private File repository;
  97. /**
  98. * The threshold above which uploads will be stored on disk.
  99. */
  100. private int sizeThreshold = DEFAULT_SIZE_THRESHOLD;
  101. // ----------------------------------------------------------- Constructors
  102. /**
  103. * Constructs an unconfigured instance of this class. The resulting factory
  104. * may be configured by calling the appropriate setter methods.
  105. */
  106. public DefaultFileItemFactory()
  107. {
  108. }
  109. /**
  110. * Constructs a preconfigured instance of this class.
  111. *
  112. * @param sizeThreshold The threshold, in bytes, below which items will be
  113. * retained in memory and above which they will be
  114. * stored as a file.
  115. * @param repository The data repository, which is the directory in
  116. * which files will be created, should the item size
  117. * exceed the threshold.
  118. */
  119. public DefaultFileItemFactory(int sizeThreshold, File repository)
  120. {
  121. this.sizeThreshold = sizeThreshold;
  122. this.repository = repository;
  123. }
  124. // ------------------------------------------------------------- Properties
  125. /**
  126. * Returns the directory used to temporarily store files that are larger
  127. * than the configured size threshold.
  128. *
  129. * @return The directory in which temporary files will be located.
  130. *
  131. * @see #setRepository(java.io.File)
  132. *
  133. */
  134. public File getRepository()
  135. {
  136. return repository;
  137. }
  138. /**
  139. * Sets the directory used to temporarily store files that are larger
  140. * than the configured size threshold.
  141. *
  142. * @param repository The directory in which temporary files will be located.
  143. *
  144. * @see #getRepository()
  145. *
  146. */
  147. public void setRepository(File repository)
  148. {
  149. this.repository = repository;
  150. }
  151. /**
  152. * Returns the size threshold beyond which files are written directly to
  153. * disk. The default value is 1024 bytes.
  154. *
  155. * @return The size threshold, in bytes.
  156. *
  157. * @see #setSizeThreshold(int)
  158. */
  159. public int getSizeThreshold()
  160. {
  161. return sizeThreshold;
  162. }
  163. /**
  164. * Sets the size threshold beyond which files are written directly to disk.
  165. *
  166. * @param sizeThreshold The size threshold, in bytes.
  167. *
  168. * @see #getSizeThreshold()
  169. *
  170. */
  171. public void setSizeThreshold(int sizeThreshold)
  172. {
  173. this.sizeThreshold = sizeThreshold;
  174. }
  175. // --------------------------------------------------------- Public Methods
  176. /**
  177. * Create a new {@link org.apache.commons.fileupload.DefaultFileItem}
  178. * instance from the supplied parameters and the local factory
  179. * configuration.
  180. *
  181. * @param fieldName The name of the form field.
  182. * @param contentType The content type of the form field.
  183. * @param isFormField <code>true</code> if this is a plain form field;
  184. * <code>false</code> otherwise.
  185. * @param fileName The name of the uploaded file, if any, as supplied
  186. * by the browser or other client.
  187. *
  188. * @return The newly created file item.
  189. */
  190. public FileItem createItem(
  191. String fieldName,
  192. String contentType,
  193. boolean isFormField,
  194. String fileName
  195. )
  196. {
  197. return new DefaultFileItem(fieldName, contentType,
  198. isFormField, fileName, sizeThreshold, repository);
  199. }
  200. }