FileDocCategorySizeDatePackage
MultipartEntity.javaAPI DocAndroid 1.5 API8447Wed May 06 22:41:56 BST 2009com.android.internal.http.multipart

MultipartEntity

public class MultipartEntity extends AbstractHttpEntity
Implements a request entity suitable for an HTTP multipart POST method.

The HTTP multipart POST method is defined in section 3.3 of RFC1867:

The media-type multipart/form-data follows the rules of all multipart MIME data streams as outlined in RFC 1521. The multipart/form-data contains a series of parts. Each part is expected to contain a content-disposition header where the value is "form-data" and a name attribute specifies the field name within the form, e.g., 'content-disposition: form-data; name="xxxxx"', where xxxxx is the field name corresponding to that field. Field names originally in non-ASCII character sets may be encoded using the method outlined in RFC 1522.

This entity is designed to be used in conjunction with the {@link org.apache.http.HttpRequest} to provide multipart posts. Example usage:

File f = new File("/path/fileToUpload.txt");
HttpRequest request = new HttpRequest("http://host/some_path");
Part[] parts = {
new StringPart("param_name", "value"),
new FilePart(f.getName(), f)
};
filePost.setEntity(
new MultipartRequestEntity(parts, filePost.getParams())
);
HttpClient client = new HttpClient();
int status = client.executeMethod(filePost);
since
3.0

Fields Summary
private static final Log
log
private static final String
MULTIPART_FORM_CONTENT_TYPE
The Content-Type for multipart/form-data.
public static final String
MULTIPART_BOUNDARY
Sets the value to use as the multipart boundary.

This parameter expects a value if type {@link String}.

private static byte[]
MULTIPART_CHARS
The pool of ASCII chars to be used for generating a multipart boundary.
protected Part[]
parts
The MIME parts as set by the constructor
private byte[]
multipartBoundary
private HttpParams
params
private boolean
contentConsumed
Constructors Summary
public MultipartEntity(Part[] parts, HttpParams params)
Creates a new multipart entity containing the given parts.

param
parts The parts to include.
param
params The params of the HttpMethod using this entity.

    
                                  
               
      if (parts == null) {
          throw new IllegalArgumentException("parts cannot be null");
      }
      if (params == null) {
          throw new IllegalArgumentException("params cannot be null");
      }
      this.parts = parts;
      this.params = params;
    
public MultipartEntity(Part[] parts)

      setContentType(MULTIPART_FORM_CONTENT_TYPE);
      if (parts == null) {
          throw new IllegalArgumentException("parts cannot be null");
      }
      this.parts = parts;
      this.params = null;
    
Methods Summary
private static byte[]generateMultipartBoundary()
Generates a random multipart boundary string.

    
              
        
        Random rand = new Random();
        byte[] bytes = new byte[rand.nextInt(11) + 30]; // a random size from 30 to 40
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)];
        }
        return bytes;
    
public java.io.InputStreamgetContent()

          if(!isRepeatable() && this.contentConsumed ) {
              throw new IllegalStateException("Content has been consumed");
          }
          this.contentConsumed = true;
          
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          Part.sendParts(baos, this.parts, this.multipartBoundary);
          ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
          return bais;
    
public longgetContentLength()

        try {
            return Part.getLengthOfParts(parts, getMultipartBoundary());            
        } catch (Exception e) {
            log.error("An exception occurred while getting the length of the parts", e);
            return 0;
        }
    
public org.apache.http.HeadergetContentType()

      StringBuffer buffer = new StringBuffer(MULTIPART_FORM_CONTENT_TYPE);
      buffer.append("; boundary=");
      buffer.append(EncodingUtils.getAsciiString(getMultipartBoundary()));
      return new BasicHeader(HTTP.CONTENT_TYPE, buffer.toString());

    
protected byte[]getMultipartBoundary()
Returns the MIME boundary string that is used to demarcate boundaries of this part. The first call to this method will implicitly create a new boundary string. To create a boundary string first the HttpMethodParams.MULTIPART_BOUNDARY parameter is considered. Otherwise a random one is generated.

return
The boundary string of this entity in ASCII encoding.

        if (multipartBoundary == null) {
            String temp = null;
            if (params != null) {
              temp = (String) params.getParameter(MULTIPART_BOUNDARY);
            }
            if (temp != null) {
                multipartBoundary = EncodingUtils.getAsciiBytes(temp);
            } else {
                multipartBoundary = generateMultipartBoundary();
            }
        }
        return multipartBoundary;
    
public booleanisRepeatable()
Returns true if all parts are repeatable, false otherwise.

        for (int i = 0; i < parts.length; i++) {
            if (!parts[i].isRepeatable()) {
                return false;
            }
        }
        return true;
    
public booleanisStreaming()

        return false;
    
public voidwriteTo(java.io.OutputStream out)

        Part.sendParts(out, parts, getMultipartBoundary());