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