1. /*
  2. * $Header: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/FileItem.java,v 1.15 2003/06/01 17:33:24 martinc Exp $
  3. * $Revision: 1.15 $
  4. * $Date: 2003/06/01 17:33:24 $
  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.io.IOException;
  64. import java.io.InputStream;
  65. import java.io.OutputStream;
  66. import java.io.Serializable;
  67. import java.io.UnsupportedEncodingException;
  68. /**
  69. * <p> This class represents a file or form item that was received within a
  70. * <code>multipart/form-data</code> POST request.
  71. *
  72. * <p> After retrieving an instance of this class from a {@link
  73. * org.apache.commons.fileupload.FileUpload FileUpload} instance (see
  74. * {@link org.apache.commons.fileupload.FileUpload
  75. * #parseRequest(javax.servlet.http.HttpServletRequest)}), you may
  76. * either request all contents of the file at once using {@link #get()} or
  77. * request an {@link java.io.InputStream InputStream} with
  78. * {@link #getInputStream()} and process the file without attempting to load
  79. * it into memory, which may come handy with large files.
  80. *
  81. * <p> While this interface does not extend
  82. * <code>javax.activation.DataSource</code> per se (to avoid a seldom used
  83. * dependency), several of the defined methods are specifically defined with
  84. * the same signatures as methods in that interface. This allows an
  85. * implementation of this interface to also implement
  86. * <code>javax.activation.DataSource</code> with minimal additional work.
  87. *
  88. * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
  89. * @author <a href="mailto:sean@informage.net">Sean Legassick</a>
  90. * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
  91. * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
  92. *
  93. * @version $Id: FileItem.java,v 1.15 2003/06/01 17:33:24 martinc Exp $
  94. */
  95. public interface FileItem
  96. extends Serializable
  97. {
  98. // ------------------------------- Methods from javax.activation.DataSource
  99. /**
  100. * Returns an {@link java.io.InputStream InputStream} that can be
  101. * used to retrieve the contents of the file.
  102. *
  103. * @return An {@link java.io.InputStream InputStream} that can be
  104. * used to retrieve the contents of the file.
  105. *
  106. * @exception IOException if an error occurs.
  107. */
  108. InputStream getInputStream()
  109. throws IOException;
  110. /**
  111. * Returns the content type passed by the browser or <code>null</code> if
  112. * not defined.
  113. *
  114. * @return The content type passed by the browser or <code>null</code> if
  115. * not defined.
  116. */
  117. String getContentType();
  118. /**
  119. * Returns the original filename in the client's filesystem, as provided by
  120. * the browser (or other client software). In most cases, this will be the
  121. * base file name, without path information. However, some clients, such as
  122. * the Opera browser, do include path information.
  123. *
  124. * @return The original filename in the client's filesystem.
  125. */
  126. String getName();
  127. // ------------------------------------------------------- FileItem methods
  128. /**
  129. * Provides a hint as to whether or not the file contents will be read
  130. * from memory.
  131. *
  132. * @return <code>true</code> if the file contents will be read from memory;
  133. * <code>false</code> otherwise.
  134. */
  135. boolean isInMemory();
  136. /**
  137. * Returns the size of the file item.
  138. *
  139. * @return The size of the file item, in bytes.
  140. */
  141. long getSize();
  142. /**
  143. * Returns the contents of the file item as an array of bytes.
  144. *
  145. * @return The contents of the file item as an array of bytes.
  146. */
  147. byte[] get();
  148. /**
  149. * Returns the contents of the file item as a String, using the specified
  150. * encoding. This method uses {@link #get()} to retrieve the
  151. * contents of the item.
  152. *
  153. * @param encoding The character encoding to use.
  154. *
  155. * @return The contents of the item, as a string.
  156. *
  157. * @exception UnsupportedEncodingException if the requested character
  158. * encoding is not available.
  159. */
  160. String getString(String encoding)
  161. throws UnsupportedEncodingException;
  162. /**
  163. * Returns the contents of the file item as a String, using the default
  164. * character encoding. This method uses {@link #get()} to retrieve the
  165. * contents of the item.
  166. *
  167. * @return The contents of the item, as a string.
  168. */
  169. String getString();
  170. /**
  171. * A convenience method to write an uploaded item to disk. The client code
  172. * is not concerned with whether or not the item is stored in memory, or on
  173. * disk in a temporary location. They just want to write the uploaded item
  174. * to a file.
  175. * <p>
  176. * This method is not guaranteed to succeed if called more than once for
  177. * the same item. This allows a particular implementation to use, for
  178. * example, file renaming, where possible, rather than copying all of the
  179. * underlying data, thus gaining a significant performance benefit.
  180. *
  181. * @param file The <code>File</code> into which the uploaded item should
  182. * be stored.
  183. *
  184. * @exception Exception if an error occurs.
  185. */
  186. void write(File file) throws Exception;
  187. /**
  188. * Deletes the underlying storage for a file item, including deleting any
  189. * associated temporary disk file. Although this storage will be deleted
  190. * automatically when the <code>FileItem</code> instance is garbage
  191. * collected, this method can be used to ensure that this is done at an
  192. * earlier time, thus preserving system resources.
  193. */
  194. void delete();
  195. /**
  196. * Returns the name of the field in the multipart form corresponding to
  197. * this file item.
  198. *
  199. * @return The name of the form field.
  200. */
  201. String getFieldName();
  202. /**
  203. * Sets the field name used to reference this file item.
  204. *
  205. * @param name The name of the form field.
  206. */
  207. void setFieldName(String name);
  208. /**
  209. * Determines whether or not a <code>FileItem</code> instance represents
  210. * a simple form field.
  211. *
  212. * @return <code>true</code> if the instance represents a simple form
  213. * field; <code>false</code> if it represents an uploaded file.
  214. */
  215. boolean isFormField();
  216. /**
  217. * Specifies whether or not a <code>FileItem</code> instance represents
  218. * a simple form field.
  219. *
  220. * @param state <code>true</code> if the instance represents a simple form
  221. * field; <code>false</code> if it represents an uploaded file.
  222. */
  223. void setFormField(boolean state);
  224. /**
  225. * Returns an {@link java.io.OutputStream OutputStream} that can
  226. * be used for storing the contents of the file.
  227. *
  228. * @return An {@link java.io.OutputStream OutputStream} that can be used
  229. * for storing the contensts of the file.
  230. *
  231. * @exception IOException if an error occurs.
  232. */
  233. OutputStream getOutputStream() throws IOException;
  234. }