Class MultipartBody

java.lang.Object
org.apache.juneau.http.entity.MultipartBody
All Implemented Interfaces:
HttpBody

public final class MultipartBody extends Object implements HttpBody
An HttpBody that serializes a multipart/form-data message (RFC 7578).

Each part is a MultipartBody.MultipartPart that carries a field name, an optional filename, an optional per-part Content-Type, and a body writer. When writeTo(OutputStream) is called the parts are streamed directly — no full-body buffering occurs.

Example:

body = MultipartBody.builder() .field("title", "My Report") .file("attachment", reportFile, "application/pdf") .build();

isRepeatable() returns false when any part wraps a one-shot stream, and true when all parts are repeatable (e.g. strings, files, byte arrays).

See Also:
Since:
9.2.1
  • Method Details

    • builder

      public static MultipartBody.Builder builder()
      Returns a new MultipartBody.Builder.
    • getContentType

      Description copied from interface: HttpBody
      Returns the MIME content type of this body, e.g. "application/json; charset=UTF-8".
      Specified by:
      getContentType in interface HttpBody
      Returns:
      The content type, or null if unknown.
    • getContentLength

      public long getContentLength()
      Returns -1 — multipart bodies are streamed without pre-computing the total length.
      Specified by:
      getContentLength in interface HttpBody
      Returns:
      The content length, or -1 if unknown.
    • writeTo

      public void writeTo(OutputStream out) throws IOException
      Description copied from interface: HttpBody
      Writes the body content to the given output stream.

      Implementations must not close out.

      Specified by:
      writeTo in interface HttpBody
      Parameters:
      out - The stream to write to. Never null.
      Throws:
      IOException - If an I/O error occurs.
    • isRepeatable

      public boolean isRepeatable()
      Description copied from interface: HttpBody
      Returns true if this body can be written more than once.

      Streaming bodies backed by a one-shot InputStream should return false. Buffered bodies (byte arrays, strings, files) should return true.

      Specified by:
      isRepeatable in interface HttpBody
      Returns:
      true if repeatable.
    • getBoundary

      public String getBoundary()
      Returns the multipart boundary string.
      Returns:
      The boundary. Never null.
    • getParts

      Returns an unmodifiable view of the parts in this multipart body.
      Returns:
      The parts. Never null.