1 30 package org.apache.commons.httpclient.methods; 31 32 import java.io.File ; 33 import java.io.FileInputStream ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.OutputStream ; 37 38 import org.apache.commons.httpclient.methods.RequestEntity; 39 40 45 public class FileRequestEntity implements RequestEntity { 46 47 final File file; 48 final String contentType; 49 50 public FileRequestEntity(final File file, final String contentType) { 51 super(); 52 if (file == null) { 53 throw new IllegalArgumentException ("File may not be null"); 54 } 55 this.file = file; 56 this.contentType = contentType; 57 } 58 public long getContentLength() { 59 return this.file.length(); 60 } 61 62 public String getContentType() { 63 return this.contentType; 64 } 65 66 public boolean isRepeatable() { 67 return true; 68 } 69 70 public void writeRequest(final OutputStream out) throws IOException { 71 byte[] tmp = new byte[4096]; 72 int i = 0; 73 InputStream instream = new FileInputStream (this.file); 74 try { 75 while ((i = instream.read(tmp)) >= 0) { 76 out.write(tmp, 0, i); 77 } 78 } finally { 79 instream.close(); 80 } 81 } 82 83 } 84 | Popular Tags |