View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java,v 1.14 2003/03/16 12:05:03 olegk Exp $ 3 * $Revision: 1.14 $ 4 * $Date: 2003/03/16 12:05:03 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 2002-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 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 package org.apache.commons.httpclient.methods.multipart; 65 66 import java.io.File; 67 import java.io.FileNotFoundException; 68 import java.io.IOException; 69 import java.io.InputStream; 70 import java.io.OutputStream; 71 import org.apache.commons.httpclient.HttpConstants; 72 import org.apache.commons.logging.Log; 73 import org.apache.commons.logging.LogFactory; 74 75 /*** 76 * This class implements a part of a Multipart post object that 77 * consists of a file. 78 * 79 * @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a> 80 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> 81 * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a> 82 * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a> 83 * @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a> 84 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 85 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 86 * 87 * @since 2.0 88 * 89 */ 90 public class FilePart extends Part { 91 92 /*** Default content encoding of file attachments. */ 93 public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream"; 94 95 /*** Default charset of file attachments. */ 96 public static final String DEFAULT_CHARSET = HttpConstants.DEFAULT_CONTENT_CHARSET; 97 98 /*** Default transfer encoding of file attachments. */ 99 public static final String DEFAULT_TRANSFER_ENCODING = "binary"; 100 101 /*** Log object for this class. */ 102 private static final Log LOG = LogFactory.getLog(FilePart.class); 103 104 105 /*** Attachment's file name */ 106 protected static final String FILE_NAME = "; filename="; 107 108 /*** Attachment's file name as a byte array */ 109 protected static final byte[] FILE_NAME_BYTES = 110 HttpConstants.getAsciiBytes(FILE_NAME); 111 112 /*** Name of the file part. */ 113 private String name; 114 115 /*** Source of the file part. */ 116 private PartSource source; 117 118 /*** Content type of the file part. */ 119 private String contentType; 120 121 /*** Content encoding of the file part. */ 122 private String charset; 123 124 /*** 125 * FilePart Constructor. 126 * 127 * @param name the name for this part 128 * @param partSource the source for this part 129 * @param contentType the content type for this part 130 * @param charset the charset encoding for this part 131 */ 132 public FilePart(String name, PartSource partSource, String contentType, String charset) { 133 LOG.trace("enter FilePart(String, PartSource, String, String)"); 134 if (name == null) { 135 throw new IllegalArgumentException("Name may not be null"); 136 } 137 this.name = name; 138 if (partSource == null) { 139 throw new IllegalArgumentException("Source may not be null"); 140 } 141 if (partSource.getLength() < 0) { 142 throw new IllegalArgumentException("Source length must be >= 0"); 143 } 144 this.source = partSource; 145 if (contentType != null) { 146 this.contentType = contentType; 147 } else { 148 this.contentType = DEFAULT_CONTENT_TYPE; 149 } 150 this.charset = charset; 151 } 152 153 /*** 154 * FilePart Constructor. 155 * 156 * @param name the name for this part 157 * @param partSource the source for this part 158 */ 159 public FilePart(String name, PartSource partSource) { 160 this(name, partSource, null, null); 161 } 162 163 /*** 164 * FilePart Constructor. 165 * 166 * @param name the name of the file part 167 * @param file the file to post 168 * 169 * @throws FileNotFoundException if the <i>file</i> is not a normal 170 * file or if it is not readable. 171 */ 172 public FilePart(String name, File file) 173 throws FileNotFoundException { 174 this(name, new FilePartSource(file), null, null); 175 } 176 177 /*** 178 * FilePart Constructor. 179 * 180 * @param name the name of the file part 181 * @param file the file to post 182 * @param contentType the content type for the file 183 * @param charset the charset encoding of the file 184 * 185 * @throws FileNotFoundException if the <i>file</i> is not a normal 186 * file or if it is not readable. 187 */ 188 public FilePart(String name, File file, String contentType, String charset) 189 throws FileNotFoundException { 190 this(name, new FilePartSource(file), contentType, charset); 191 } 192 193 /*** 194 * FilePart Constructor. 195 * 196 * @param name the name of the file part 197 * @param fileName the file name 198 * @param file the file to post 199 * 200 * @throws FileNotFoundException if the <i>file</i> is not a normal 201 * file or if it is not readable. 202 */ 203 public FilePart(String name, String fileName, File file) 204 throws FileNotFoundException { 205 this(name, new FilePartSource(fileName, file), null, null); 206 } 207 208 /*** 209 * FilePart Constructor. 210 * 211 * @param name the name of the file part 212 * @param fileName the file name 213 * @param file the file to post 214 * @param contentType the content type for the file 215 * @param charset the charset encoding of the file 216 * 217 * @throws FileNotFoundException if the <i>file</i> is not a normal 218 * file or if it is not readable. 219 */ 220 public FilePart(String name, String fileName, File file, String contentType, String charset) 221 throws FileNotFoundException { 222 this(name, new FilePartSource(fileName, file), contentType, charset); 223 } 224 225 /*** 226 * Return the name. 227 * @return The name. 228 * @see org.apache.commons.httpclient.methods.multipart.Part#getName() 229 */ 230 public String getName() { 231 return this.name; 232 } 233 234 /*** 235 * Return the content type of this part. 236 * @return String The name. 237 */ 238 public String getContentType() { 239 return this.contentType; 240 } 241 242 /*** 243 * Return the character encoding of this part. 244 * @return String The name. 245 */ 246 public String getCharSet() { 247 return this.charset; 248 } 249 250 /*** 251 * Return the transfer encoding of this part. 252 * @return String The name. 253 */ 254 255 public String getTransferEncoding() { 256 return DEFAULT_TRANSFER_ENCODING; 257 } 258 259 /*** 260 * Write the disposition header to the output stream 261 * @param out The output stream 262 * @throws IOException If an IO problem occurs 263 * @see Part#sendDispositionHeader(OutputStream) 264 */ 265 protected void sendDispositionHeader(OutputStream out) 266 throws IOException { 267 LOG.trace("enter sendDispositionHeader(OutputStream out)"); 268 super.sendDispositionHeader(out); 269 String filename = this.source.getFileName(); 270 if (filename != null) { 271 out.write(FILE_NAME_BYTES); 272 out.write(QUOTE_BYTES); 273 out.write(HttpConstants.getAsciiBytes(filename)); 274 out.write(QUOTE_BYTES); 275 } 276 } 277 278 /*** 279 * Write the data in "source" to the specified stream. 280 * @param out The output stream. 281 * @throws IOException if an IO problem occurs. 282 * @see org.apache.commons.httpclient.methods.multipart.Part#sendData(OutputStream) 283 */ 284 protected void sendData(OutputStream out) throws IOException { 285 LOG.trace("enter sendData(OutputStream out)"); 286 if (lengthOfData() == 0) { 287 288 // this file contains no data, so there is nothing to send. 289 // we don't want to create a zero length buffer as this will 290 // cause an infinite loop when reading. 291 LOG.debug("No data to send."); 292 return; 293 } 294 295 byte[] tmp = new byte[4096]; 296 InputStream instream = source.createInputStream(); 297 try { 298 int len; 299 while ((len = instream.read(tmp)) >= 0) { 300 out.write(tmp, 0, len); 301 } 302 } finally { 303 // we're done with the stream, close it 304 instream.close(); 305 } 306 } 307 308 /*** 309 * Return the length of the data. 310 * @return The length. 311 * @throws IOException if an IO problem occurs 312 * @see org.apache.commons.httpclient.methods.multipart.Part#lengthOfData() 313 */ 314 protected long lengthOfData() throws IOException { 315 LOG.trace("enter lengthOfData()"); 316 return source.getLength(); 317 } 318 319 }

This page was automatically generated by Maven