1 31 32 package org.apache.commons.httpclient.methods; 33 34 import java.io.File ; 35 import java.io.FileNotFoundException ; 36 import java.io.IOException ; 37 import java.io.OutputStream ; 38 import java.util.ArrayList ; 39 import java.util.List ; 40 41 import org.apache.commons.httpclient.HttpConnection; 42 import org.apache.commons.httpclient.HttpException; 43 import org.apache.commons.httpclient.HttpState; 44 import org.apache.commons.httpclient.methods.multipart.FilePart; 45 import org.apache.commons.httpclient.methods.multipart.Part; 46 import org.apache.commons.httpclient.methods.multipart.StringPart; 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 50 77 public class MultipartPostMethod extends ExpectContinueMethod { 78 79 80 public static final String MULTIPART_FORM_CONTENT_TYPE = 81 "multipart/form-data"; 82 83 84 private static final Log LOG = LogFactory.getLog(MultipartPostMethod.class); 85 86 87 private final List parameters = new ArrayList (); 88 89 92 public MultipartPostMethod() { 93 super(); 94 } 95 96 101 public MultipartPostMethod(String uri) { 102 super(uri); 103 } 104 105 111 public MultipartPostMethod(String uri, String tempDir) { 112 super(uri, tempDir); 113 } 114 115 122 public MultipartPostMethod(String uri, String tempDir, String tempFile) { 123 super(uri, tempDir, tempFile); 124 } 125 126 133 protected boolean hasRequestContent() { 134 return true; 135 } 136 137 141 public String getName() { 142 return "POST"; 143 } 144 145 151 public void addParameter(String parameterName, String parameterValue) { 152 LOG.trace("enter addParameter(String parameterName, String parameterValue)"); 153 Part param = new StringPart(parameterName, parameterValue); 154 parameters.add(param); 155 } 156 157 164 public void addParameter(String parameterName, File parameterFile) 165 throws FileNotFoundException { 166 LOG.trace("enter MultipartPostMethod.addParameter(String parameterName, " 167 + "File parameterFile)"); 168 Part param = new FilePart(parameterName, parameterFile); 169 parameters.add(param); 170 } 171 172 180 public void addParameter(String parameterName, String fileName, File parameterFile) 181 throws FileNotFoundException { 182 LOG.trace("enter MultipartPostMethod.addParameter(String parameterName, " 183 + "String fileName, File parameterFile)"); 184 Part param = new FilePart(parameterName, fileName, parameterFile); 185 parameters.add(param); 186 } 187 188 193 public void addPart (final Part part) { 194 LOG.trace("enter addPart(Part part)"); 195 parameters.add(part); 196 } 197 198 203 public Part[] getParts() { 204 return (Part[]) parameters.toArray(new Part[parameters.size()]); 205 } 206 220 protected void addRequestHeaders(HttpState state, HttpConnection conn) 221 throws IOException , HttpException { 222 LOG.trace("enter MultipartPostMethod.addRequestHeaders(HttpState state, " 223 + "HttpConnection conn)"); 224 super.addRequestHeaders(state, conn); 225 226 if (!parameters.isEmpty()) { 227 StringBuffer buffer = new StringBuffer (MULTIPART_FORM_CONTENT_TYPE); 228 if (Part.getBoundary() != null) { 229 buffer.append("; boundary="); 230 buffer.append(Part.getBoundary()); 231 } 232 setRequestHeader("Content-Type", buffer.toString()); 233 } 234 } 235 236 251 protected boolean writeRequestBody(HttpState state, HttpConnection conn) 252 throws IOException , HttpException { 253 LOG.trace("enter MultipartPostMethod.writeRequestBody(HttpState state, " 254 + "HttpConnection conn)"); 255 OutputStream out = conn.getRequestOutputStream(); 256 Part.sendParts(out, getParts()); 257 return true; 258 } 259 260 268 protected int getRequestContentLength() { 269 LOG.trace("enter MultipartPostMethod.getRequestContentLength()"); 270 try { 271 long len = Part.getLengthOfParts(getParts()); 272 if (len <= Integer.MAX_VALUE) { 274 return (int) len; 275 } else { 276 return (Integer.MAX_VALUE); 277 } 278 } catch (IOException e) { 279 throw new RuntimeException (e.toString()); 281 } 282 } 283 284 285 296 public void recycle() { 297 LOG.trace("enter MultipartPostMethod.recycle()"); 298 super.recycle(); 299 parameters.clear(); 300 } 301 } 302 | Popular Tags |