1 31 32 package org.apache.commons.httpclient.methods.multipart; 33 34 import java.io.File ; 35 import java.io.FileNotFoundException ; 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 import java.io.OutputStream ; 39 import org.apache.commons.httpclient.HttpConstants; 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 43 58 public class FilePart extends PartBase { 59 60 61 public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream"; 62 63 64 public static final String DEFAULT_CHARSET = HttpConstants.DEFAULT_CONTENT_CHARSET; 65 66 67 public static final String DEFAULT_TRANSFER_ENCODING = "binary"; 68 69 70 private static final Log LOG = LogFactory.getLog(FilePart.class); 71 72 73 protected static final String FILE_NAME = "; filename="; 74 75 76 protected static final byte[] FILE_NAME_BYTES = 77 HttpConstants.getAsciiBytes(FILE_NAME); 78 79 80 private PartSource source; 81 82 92 public FilePart(String name, PartSource partSource, String contentType, String charset) { 93 94 super( 95 name, 96 contentType == null ? DEFAULT_CONTENT_TYPE : contentType, 97 charset == null ? DEFAULT_CHARSET : charset, 98 DEFAULT_TRANSFER_ENCODING 99 ); 100 101 if (partSource == null) { 102 throw new IllegalArgumentException ("Source may not be null"); 103 } 104 if (partSource.getLength() < 0) { 105 throw new IllegalArgumentException ("Source length must be >= 0"); 106 } 107 this.source = partSource; 108 } 109 110 116 public FilePart(String name, PartSource partSource) { 117 this(name, partSource, null, null); 118 } 119 120 129 public FilePart(String name, File file) 130 throws FileNotFoundException { 131 this(name, new FilePartSource(file), null, null); 132 } 133 134 147 public FilePart(String name, File file, String contentType, String charset) 148 throws FileNotFoundException { 149 this(name, new FilePartSource(file), contentType, charset); 150 } 151 152 162 public FilePart(String name, String fileName, File file) 163 throws FileNotFoundException { 164 this(name, new FilePartSource(fileName, file), null, null); 165 } 166 167 181 public FilePart(String name, String fileName, File file, String contentType, String charset) 182 throws FileNotFoundException { 183 this(name, new FilePartSource(fileName, file), contentType, charset); 184 } 185 186 192 protected void sendDispositionHeader(OutputStream out) 193 throws IOException { 194 LOG.trace("enter sendDispositionHeader(OutputStream out)"); 195 super.sendDispositionHeader(out); 196 String filename = this.source.getFileName(); 197 if (filename != null) { 198 out.write(FILE_NAME_BYTES); 199 out.write(QUOTE_BYTES); 200 out.write(HttpConstants.getAsciiBytes(filename)); 201 out.write(QUOTE_BYTES); 202 } 203 } 204 205 211 protected void sendData(OutputStream out) throws IOException { 212 LOG.trace("enter sendData(OutputStream out)"); 213 if (lengthOfData() == 0) { 214 215 LOG.debug("No data to send."); 219 return; 220 } 221 222 byte[] tmp = new byte[4096]; 223 InputStream instream = source.createInputStream(); 224 try { 225 int len; 226 while ((len = instream.read(tmp)) >= 0) { 227 out.write(tmp, 0, len); 228 } 229 } finally { 230 instream.close(); 232 } 233 } 234 235 240 protected PartSource getSource() { 241 LOG.trace("enter getSource()"); 242 return this.source; 243 } 244 245 251 protected long lengthOfData() throws IOException { 252 LOG.trace("enter lengthOfData()"); 253 return source.getLength(); 254 } 255 256 } 257 | Popular Tags |