Class BodyPublisherMultipartBodyBuilder


  • public final class BodyPublisherMultipartBodyBuilder
    extends Object
    Builder for a multipart body that uses a HttpRequest.BodyPublisher to represent the data. Intended for use with Java 11+ HttpClient.
    Since:
    8.5.0
    • Constructor Detail

      • BodyPublisherMultipartBodyBuilder

        public BodyPublisherMultipartBodyBuilder()
        Creates a new multipart builder with a random boundary.
      • BodyPublisherMultipartBodyBuilder

        public BodyPublisherMultipartBodyBuilder​(String boundary)
        Creates a new multipart builder with the given boundary.
        Parameters:
        boundary - The boundary string to use.
    • Method Detail

      • addBinary

        @CanIgnoreReturnValue
        public BodyPublisherMultipartBodyBuilder addBinary​(String name,
                                                           String fileName,
                                                           String contentType,
                                                           long contentLength,
                                                           IInputStreamSupplier data)
        Adds a file upload part to the list of parts.
        Parameters:
        name - The name of the form field.
        fileName - The filename to report in the Content-Disposition header.
        contentType - The content type of the file.
        data - The InputStream supplier for the file data.
      • addText

        @CanIgnoreReturnValue
        public BodyPublisherMultipartBodyBuilder addText​(String name,
                                                         String value)
        Adds a simple text form field part.
        Parameters:
        name - The name of the form field.
        value - The value of the form field.
        Returns:
        This builder for method chaining.
      • boundary

        public String boundary()
        Gets the boundary string used to separate parts.
        Returns:
        The boundary string.
      • build

        public IMultipartBody<HttpRequest.BodyPublisher> build()
        Builds the final multipart BodyPublisher.

        This publisher streams binary parts and concatenates everything lazily. Does not buffer the entire multipart body in memory.