Class MultipartBody

java.lang.Object
cloud.opencode.base.web.body.MultipartBody
All Implemented Interfaces:
RequestBody

public final class MultipartBody extends Object implements RequestBody
Multipart Body - multipart/form-data Request Body Builder Multipart 请求体 - multipart/form-data 请求体构建器

Builds multipart/form-data encoded request bodies for HTTP file uploads and mixed form submissions using the Builder pattern.

使用 Builder 模式构建 multipart/form-data 编码的请求体,用于 HTTP 文件上传和混合表单提交。

Features | 主要功能:

  • Builder pattern for fluent construction - 构建器模式支持流式构建
  • Text field support - 文本字段支持
  • File upload with byte[], Path, or InputStream - 文件上传支持 byte[]、Path 或 InputStream
  • Automatic content type detection for Path - Path 自动检测内容类型
  • UUID-based boundary generation - 基于 UUID 的 boundary 生成
  • Immutable after construction - 构建后不可变

Usage Examples | 使用示例:

// Build multipart body with text and file
MultipartBody body = MultipartBody.builder()
    .addField("name", "John")
    .addFile("avatar", "avatar.png", "image/png", imageBytes)
    .addFile("document", Path.of("report.pdf"))
    .build();

String contentType = body.getContentType();
BodyPublisher publisher = body.getBodyPublisher();

Security | 安全性:

  • Thread-safe: Yes (immutable after construction) - 线程安全: 是(构建后不可变)
  • Null-safe: No (name and value must not be null) - 空值安全: 否(名称和值不能为 null)
Since:
JDK 25, opencode-base-web V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • builder

      public static MultipartBody.Builder builder()
      Creates a new builder with a random UUID boundary. 创建使用随机 UUID boundary 的新构建器。
      Returns:
      the builder | 构建器
    • builder

      public static MultipartBody.Builder builder(String boundary)
      Creates a new builder with the specified boundary. 创建使用指定 boundary 的新构建器。
      Parameters:
      boundary - the boundary string | boundary 字符串
      Returns:
      the builder | 构建器
    • getContentType

      public String getContentType()
      Specified by:
      getContentType in interface RequestBody
    • getBodyPublisher

      public HttpRequest.BodyPublisher getBodyPublisher()
      Specified by:
      getBodyPublisher in interface RequestBody
    • getContentLength

      public long getContentLength()
      Specified by:
      getContentLength in interface RequestBody
    • getBoundary

      public String getBoundary()
      Gets the boundary string. 获取 boundary 字符串。
      Returns:
      the boundary | boundary
    • getParts

      public List<MultipartBody.Part> getParts()
      Gets the immutable list of parts. 获取不可变的 part 列表。
      Returns:
      the parts | part 列表
    • toString

      public String toString()
      Overrides:
      toString in class Object