Class MultipartBody
java.lang.Object
cloud.opencode.base.web.body.MultipartBody
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for MultipartBody MultipartBody 构建器static final recordMultipart Part - Represents a single part in a multipart body Multipart Part - 表示 multipart 请求体中的单个 partNested classes/interfaces inherited from interface RequestBody
RequestBody.SimpleBody -
Method Summary
Modifier and TypeMethodDescriptionstatic MultipartBody.Builderbuilder()Creates a new builder with a random UUID boundary.static MultipartBody.BuilderCreates a new builder with the specified boundary.Gets the boundary string.longgetParts()Gets the immutable list of parts.toString()
-
Method Details
-
builder
Creates a new builder with a random UUID boundary. 创建使用随机 UUID boundary 的新构建器。- Returns:
- the builder | 构建器
-
builder
Creates a new builder with the specified boundary. 创建使用指定 boundary 的新构建器。- Parameters:
boundary- the boundary string | boundary 字符串- Returns:
- the builder | 构建器
-
getContentType
- Specified by:
getContentTypein interfaceRequestBody
-
getBodyPublisher
- Specified by:
getBodyPublisherin interfaceRequestBody
-
getContentLength
public long getContentLength()- Specified by:
getContentLengthin interfaceRequestBody
-
getBoundary
Gets the boundary string. 获取 boundary 字符串。- Returns:
- the boundary | boundary
-
getParts
Gets the immutable list of parts. 获取不可变的 part 列表。- Returns:
- the parts | part 列表
-
toString
-