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 */
017package org.apache.commons.fileupload;
018
019import java.io.File;
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023import java.io.UnsupportedEncodingException;
024
025/**
026 * <p> This class represents a file or form item that was received within a
027 * <code>multipart/form-data</code> POST request.
028 *
029 * <p> After retrieving an instance of this class from a {@link
030 * org.apache.commons.fileupload.FileUpload FileUpload} instance (see
031 * {@link org.apache.commons.fileupload.servlet.ServletFileUpload
032 * #parseRequest(javax.servlet.http.HttpServletRequest)}), you may
033 * either request all contents of the file at once using {@link #get()} or
034 * request an {@link java.io.InputStream InputStream} with
035 * {@link #getInputStream()} and process the file without attempting to load
036 * it into memory, which may come handy with large files.
037 *
038 * <p> While this interface does not extend
039 * <code>javax.activation.DataSource</code> per se (to avoid a seldom used
040 * dependency), several of the defined methods are specifically defined with
041 * the same signatures as methods in that interface. This allows an
042 * implementation of this interface to also implement
043 * <code>javax.activation.DataSource</code> with minimal additional work.
044 *
045 * @version $Id$
046 * @since 1.3 additionally implements FileItemHeadersSupport
047 */
048public interface FileItem extends FileItemHeadersSupport {
049
050    // ------------------------------- Methods from javax.activation.DataSource
051
052    /**
053     * Returns an {@link java.io.InputStream InputStream} that can be
054     * used to retrieve the contents of the file.
055     *
056     * @return An {@link java.io.InputStream InputStream} that can be
057     *         used to retrieve the contents of the file.
058     *
059     * @throws IOException if an error occurs.
060     */
061    InputStream getInputStream() throws IOException;
062
063    /**
064     * Returns the content type passed by the browser or <code>null</code> if
065     * not defined.
066     *
067     * @return The content type passed by the browser or <code>null</code> if
068     *         not defined.
069     */
070    String getContentType();
071
072    /**
073     * Returns the original filename in the client's filesystem, as provided by
074     * the browser (or other client software). In most cases, this will be the
075     * base file name, without path information. However, some clients, such as
076     * the Opera browser, do include path information.
077     *
078     * @return The original filename in the client's filesystem.
079     * @throws InvalidFileNameException The file name contains a NUL character,
080     *   which might be an indicator of a security attack. If you intend to
081     *   use the file name anyways, catch the exception and use
082     *   InvalidFileNameException#getName().
083     */
084    String getName();
085
086    // ------------------------------------------------------- FileItem methods
087
088    /**
089     * Provides a hint as to whether or not the file contents will be read
090     * from memory.
091     *
092     * @return <code>true</code> if the file contents will be read from memory;
093     *         <code>false</code> otherwise.
094     */
095    boolean isInMemory();
096
097    /**
098     * Returns the size of the file item.
099     *
100     * @return The size of the file item, in bytes.
101     */
102    long getSize();
103
104    /**
105     * Returns the contents of the file item as an array of bytes.
106     *
107     * @return The contents of the file item as an array of bytes.
108     */
109    byte[] get();
110
111    /**
112     * Returns the contents of the file item as a String, using the specified
113     * encoding.  This method uses {@link #get()} to retrieve the
114     * contents of the item.
115     *
116     * @param encoding The character encoding to use.
117     *
118     * @return The contents of the item, as a string.
119     *
120     * @throws UnsupportedEncodingException if the requested character
121     *                                      encoding is not available.
122     */
123    String getString(String encoding) throws UnsupportedEncodingException;
124
125    /**
126     * Returns the contents of the file item as a String, using the default
127     * character encoding.  This method uses {@link #get()} to retrieve the
128     * contents of the item.
129     *
130     * @return The contents of the item, as a string.
131     */
132    String getString();
133
134    /**
135     * A convenience method to write an uploaded item to disk. The client code
136     * is not concerned with whether or not the item is stored in memory, or on
137     * disk in a temporary location. They just want to write the uploaded item
138     * to a file.
139     * <p>
140     * This method is not guaranteed to succeed if called more than once for
141     * the same item. This allows a particular implementation to use, for
142     * example, file renaming, where possible, rather than copying all of the
143     * underlying data, thus gaining a significant performance benefit.
144     *
145     * @param file The <code>File</code> into which the uploaded item should
146     *             be stored.
147     *
148     * @throws Exception if an error occurs.
149     */
150    void write(File file) throws Exception;
151
152    /**
153     * Deletes the underlying storage for a file item, including deleting any
154     * associated temporary disk file. Although this storage will be deleted
155     * automatically when the <code>FileItem</code> instance is garbage
156     * collected, this method can be used to ensure that this is done at an
157     * earlier time, thus preserving system resources.
158     */
159    void delete();
160
161    /**
162     * Returns the name of the field in the multipart form corresponding to
163     * this file item.
164     *
165     * @return The name of the form field.
166     */
167    String getFieldName();
168
169    /**
170     * Sets the field name used to reference this file item.
171     *
172     * @param name The name of the form field.
173     */
174    void setFieldName(String name);
175
176    /**
177     * Determines whether or not a <code>FileItem</code> instance represents
178     * a simple form field.
179     *
180     * @return <code>true</code> if the instance represents a simple form
181     *         field; <code>false</code> if it represents an uploaded file.
182     */
183    boolean isFormField();
184
185    /**
186     * Specifies whether or not a <code>FileItem</code> instance represents
187     * a simple form field.
188     *
189     * @param state <code>true</code> if the instance represents a simple form
190     *              field; <code>false</code> if it represents an uploaded file.
191     */
192    void setFormField(boolean state);
193
194    /**
195     * Returns an {@link java.io.OutputStream OutputStream} that can
196     * be used for storing the contents of the file.
197     *
198     * @return An {@link java.io.OutputStream OutputStream} that can be used
199     *         for storing the contensts of the file.
200     *
201     * @throws IOException if an error occurs.
202     */
203    OutputStream getOutputStream() throws IOException;
204
205}