1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 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 963609 2010-07-13 06:56:47Z jochen $ 52 */ 53 public interface FileItem extends Serializable { 54 55 56 // ------------------------------- Methods from javax.activation.DataSource 57 58 59 /** 60 * Returns an {@link java.io.InputStream InputStream} that can be 61 * used to retrieve the contents of the file. 62 * 63 * @return An {@link java.io.InputStream InputStream} that can be 64 * used to retrieve the contents of the file. 65 * 66 * @throws IOException if an error occurs. 67 */ 68 InputStream getInputStream() throws IOException; 69 70 71 /** 72 * Returns the content type passed by the browser or <code>null</code> if 73 * not defined. 74 * 75 * @return The content type passed by the browser or <code>null</code> if 76 * not defined. 77 */ 78 String getContentType(); 79 80 81 /** 82 * Returns the original filename in the client's filesystem, as provided by 83 * the browser (or other client software). In most cases, this will be the 84 * base file name, without path information. However, some clients, such as 85 * the Opera browser, do include path information. 86 * 87 * @return The original filename in the client's filesystem. 88 * @throws InvalidFileNameException The file name contains a NUL character, 89 * which might be an indicator of a security attack. If you intend to 90 * use the file name anyways, catch the exception and use 91 * InvalidFileNameException#getName(). 92 */ 93 String getName(); 94 95 96 // ------------------------------------------------------- FileItem methods 97 98 99 /** 100 * Provides a hint as to whether or not the file contents will be read 101 * from memory. 102 * 103 * @return <code>true</code> if the file contents will be read from memory; 104 * <code>false</code> otherwise. 105 */ 106 boolean isInMemory(); 107 108 109 /** 110 * Returns the size of the file item. 111 * 112 * @return The size of the file item, in bytes. 113 */ 114 long getSize(); 115 116 117 /** 118 * Returns the contents of the file item as an array of bytes. 119 * 120 * @return The contents of the file item as an array of bytes. 121 */ 122 byte[] get(); 123 124 125 /** 126 * Returns the contents of the file item as a String, using the specified 127 * encoding. This method uses {@link #get()} to retrieve the 128 * contents of the item. 129 * 130 * @param encoding The character encoding to use. 131 * 132 * @return The contents of the item, as a string. 133 * 134 * @throws UnsupportedEncodingException if the requested character 135 * encoding is not available. 136 */ 137 String getString(String encoding) throws UnsupportedEncodingException; 138 139 140 /** 141 * Returns the contents of the file item as a String, using the default 142 * character encoding. This method uses {@link #get()} to retrieve the 143 * contents of the item. 144 * 145 * @return The contents of the item, as a string. 146 */ 147 String getString(); 148 149 150 /** 151 * A convenience method to write an uploaded item to disk. The client code 152 * is not concerned with whether or not the item is stored in memory, or on 153 * disk in a temporary location. They just want to write the uploaded item 154 * to a file. 155 * <p> 156 * This method is not guaranteed to succeed if called more than once for 157 * the same item. This allows a particular implementation to use, for 158 * example, file renaming, where possible, rather than copying all of the 159 * underlying data, thus gaining a significant performance benefit. 160 * 161 * @param file The <code>File</code> into which the uploaded item should 162 * be stored. 163 * 164 * @throws Exception if an error occurs. 165 */ 166 void write(File file) throws Exception; 167 168 169 /** 170 * Deletes the underlying storage for a file item, including deleting any 171 * associated temporary disk file. Although this storage will be deleted 172 * automatically when the <code>FileItem</code> instance is garbage 173 * collected, this method can be used to ensure that this is done at an 174 * earlier time, thus preserving system resources. 175 */ 176 void delete(); 177 178 179 /** 180 * Returns the name of the field in the multipart form corresponding to 181 * this file item. 182 * 183 * @return The name of the form field. 184 */ 185 String getFieldName(); 186 187 188 /** 189 * Sets the field name used to reference this file item. 190 * 191 * @param name The name of the form field. 192 */ 193 void setFieldName(String name); 194 195 196 /** 197 * Determines whether or not a <code>FileItem</code> instance represents 198 * a simple form field. 199 * 200 * @return <code>true</code> if the instance represents a simple form 201 * field; <code>false</code> if it represents an uploaded file. 202 */ 203 boolean isFormField(); 204 205 206 /** 207 * Specifies whether or not a <code>FileItem</code> instance represents 208 * a simple form field. 209 * 210 * @param state <code>true</code> if the instance represents a simple form 211 * field; <code>false</code> if it represents an uploaded file. 212 */ 213 void setFormField(boolean state); 214 215 216 /** 217 * Returns an {@link java.io.OutputStream OutputStream} that can 218 * be used for storing the contents of the file. 219 * 220 * @return An {@link java.io.OutputStream OutputStream} that can be used 221 * for storing the contensts of the file. 222 * 223 * @throws IOException if an error occurs. 224 */ 225 OutputStream getOutputStream() throws IOException; 226 227 }