FilePart.java revision 823675fdbb7f974b8e2fa9fbb71774b32487582d
1/* 2 * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java,v 1.19 2004/04/18 23:51:37 jsdever Exp $ 3 * $Revision: 480424 $ 4 * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $ 5 * 6 * ==================================================================== 7 * 8 * Licensed to the Apache Software Foundation (ASF) under one or more 9 * contributor license agreements. See the NOTICE file distributed with 10 * this work for additional information regarding copyright ownership. 11 * The ASF licenses this file to You under the Apache License, Version 2.0 12 * (the "License"); you may not use this file except in compliance with 13 * the License. You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, software 18 * distributed under the License is distributed on an "AS IS" BASIS, 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 * See the License for the specific language governing permissions and 21 * limitations under the License. 22 * ==================================================================== 23 * 24 * This software consists of voluntary contributions made by many 25 * individuals on behalf of the Apache Software Foundation. For more 26 * information on the Apache Software Foundation, please see 27 * <http://www.apache.org/>. 28 * 29 */ 30 31package com.android.internal.http.multipart; 32 33import java.io.File; 34import java.io.FileNotFoundException; 35import java.io.IOException; 36import java.io.InputStream; 37import java.io.OutputStream; 38import org.apache.http.util.EncodingUtils; 39import org.apache.commons.logging.Log; 40import org.apache.commons.logging.LogFactory; 41 42/** 43 * This class implements a part of a Multipart post object that 44 * consists of a file. 45 * 46 * @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a> 47 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> 48 * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a> 49 * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a> 50 * @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a> 51 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 52 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 53 * 54 * @since 2.0 55 * 56 * @deprecated Please use {@link java.net.URLConnection} and friends instead. 57 * The Apache HTTP client is no longer maintained and may be removed in a future 58 * release. Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 59 * for further details. 60 */ 61@Deprecated 62public class FilePart extends PartBase { 63 64 /** Default content encoding of file attachments. */ 65 public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream"; 66 67 /** Default charset of file attachments. */ 68 public static final String DEFAULT_CHARSET = "ISO-8859-1"; 69 70 /** Default transfer encoding of file attachments. */ 71 public static final String DEFAULT_TRANSFER_ENCODING = "binary"; 72 73 /** Log object for this class. */ 74 private static final Log LOG = LogFactory.getLog(FilePart.class); 75 76 /** Attachment's file name */ 77 protected static final String FILE_NAME = "; filename="; 78 79 /** Attachment's file name as a byte array */ 80 private static final byte[] FILE_NAME_BYTES = 81 EncodingUtils.getAsciiBytes(FILE_NAME); 82 83 /** Source of the file part. */ 84 private PartSource source; 85 86 /** 87 * FilePart Constructor. 88 * 89 * @param name the name for this part 90 * @param partSource the source for this part 91 * @param contentType the content type for this part, if <code>null</code> the 92 * {@link #DEFAULT_CONTENT_TYPE default} is used 93 * @param charset the charset encoding for this part, if <code>null</code> the 94 * {@link #DEFAULT_CHARSET default} is used 95 */ 96 public FilePart(String name, PartSource partSource, String contentType, String charset) { 97 98 super( 99 name, 100 contentType == null ? DEFAULT_CONTENT_TYPE : contentType, 101 charset == null ? "ISO-8859-1" : charset, 102 DEFAULT_TRANSFER_ENCODING 103 ); 104 105 if (partSource == null) { 106 throw new IllegalArgumentException("Source may not be null"); 107 } 108 this.source = partSource; 109 } 110 111 /** 112 * FilePart Constructor. 113 * 114 * @param name the name for this part 115 * @param partSource the source for this part 116 */ 117 public FilePart(String name, PartSource partSource) { 118 this(name, partSource, null, null); 119 } 120 121 /** 122 * FilePart Constructor. 123 * 124 * @param name the name of the file part 125 * @param file the file to post 126 * 127 * @throws FileNotFoundException if the <i>file</i> is not a normal 128 * file or if it is not readable. 129 */ 130 public FilePart(String name, File file) 131 throws FileNotFoundException { 132 this(name, new FilePartSource(file), null, null); 133 } 134 135 /** 136 * FilePart Constructor. 137 * 138 * @param name the name of the file part 139 * @param file the file to post 140 * @param contentType the content type for this part, if <code>null</code> the 141 * {@link #DEFAULT_CONTENT_TYPE default} is used 142 * @param charset the charset encoding for this part, if <code>null</code> the 143 * {@link #DEFAULT_CHARSET default} is used 144 * 145 * @throws FileNotFoundException if the <i>file</i> is not a normal 146 * file or if it is not readable. 147 */ 148 public FilePart(String name, File file, String contentType, String charset) 149 throws FileNotFoundException { 150 this(name, new FilePartSource(file), contentType, charset); 151 } 152 153 /** 154 * FilePart Constructor. 155 * 156 * @param name the name of the file part 157 * @param fileName the file name 158 * @param file the file to post 159 * 160 * @throws FileNotFoundException if the <i>file</i> is not a normal 161 * file or if it is not readable. 162 */ 163 public FilePart(String name, String fileName, File file) 164 throws FileNotFoundException { 165 this(name, new FilePartSource(fileName, file), null, null); 166 } 167 168 /** 169 * FilePart Constructor. 170 * 171 * @param name the name of the file part 172 * @param fileName the file name 173 * @param file the file to post 174 * @param contentType the content type for this part, if <code>null</code> the 175 * {@link #DEFAULT_CONTENT_TYPE default} is used 176 * @param charset the charset encoding for this part, if <code>null</code> the 177 * {@link #DEFAULT_CHARSET default} is used 178 * 179 * @throws FileNotFoundException if the <i>file</i> is not a normal 180 * file or if it is not readable. 181 */ 182 public FilePart(String name, String fileName, File file, String contentType, String charset) 183 throws FileNotFoundException { 184 this(name, new FilePartSource(fileName, file), contentType, charset); 185 } 186 187 /** 188 * Write the disposition header to the output stream 189 * @param out The output stream 190 * @throws IOException If an IO problem occurs 191 * @see Part#sendDispositionHeader(OutputStream) 192 */ 193 @Override 194 protected void sendDispositionHeader(OutputStream out) 195 throws IOException { 196 LOG.trace("enter sendDispositionHeader(OutputStream out)"); 197 super.sendDispositionHeader(out); 198 String filename = this.source.getFileName(); 199 if (filename != null) { 200 out.write(FILE_NAME_BYTES); 201 out.write(QUOTE_BYTES); 202 out.write(EncodingUtils.getAsciiBytes(filename)); 203 out.write(QUOTE_BYTES); 204 } 205 } 206 207 /** 208 * Write the data in "source" to the specified stream. 209 * @param out The output stream. 210 * @throws IOException if an IO problem occurs. 211 * @see Part#sendData(OutputStream) 212 */ 213 @Override 214 protected void sendData(OutputStream out) throws IOException { 215 LOG.trace("enter sendData(OutputStream out)"); 216 if (lengthOfData() == 0) { 217 218 // this file contains no data, so there is nothing to send. 219 // we don't want to create a zero length buffer as this will 220 // cause an infinite loop when reading. 221 LOG.debug("No data to send."); 222 return; 223 } 224 225 byte[] tmp = new byte[4096]; 226 InputStream instream = source.createInputStream(); 227 try { 228 int len; 229 while ((len = instream.read(tmp)) >= 0) { 230 out.write(tmp, 0, len); 231 } 232 } finally { 233 // we're done with the stream, close it 234 instream.close(); 235 } 236 } 237 238 /** 239 * Returns the source of the file part. 240 * 241 * @return The source. 242 */ 243 protected PartSource getSource() { 244 LOG.trace("enter getSource()"); 245 return this.source; 246 } 247 248 /** 249 * Return the length of the data. 250 * @return The length. 251 * @see Part#lengthOfData() 252 */ 253 @Override 254 protected long lengthOfData() { 255 LOG.trace("enter lengthOfData()"); 256 return source.getLength(); 257 } 258 259} 260