Module bus.http

Class RequestBody

java.lang.Object
org.miaixz.bus.http.bodys.RequestBody
Direct Known Subclasses:
FormBody, MultipartBody, ProgressBody

public abstract class RequestBody extends Object
HTTP 请求体

表示 HTTP 请求的内容,支持从字符串、字节数组、文件等创建请求体。 提供媒体类型、内容长度和写入功能,支持双工和一次性传输的特殊场景。

Since:
Java 17+
Author:
Kimi Liu
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    create(org.miaixz.bus.core.lang.MediaType mediaType, byte[] content)
    从字节数组创建请求体
    create(org.miaixz.bus.core.lang.MediaType mediaType, byte[] content, int offset, int byteCount)
    从字节数组部分创建请求体
    create(org.miaixz.bus.core.lang.MediaType mediaType, File file)
    从文件创建请求体
    create(org.miaixz.bus.core.lang.MediaType mediaType, String content)
    从字符串创建请求体
    create(org.miaixz.bus.core.lang.MediaType mediaType, org.miaixz.bus.core.io.ByteString content)
    从 ByteString 创建请求体
    boolean
    检查是否为双工请求体
    boolean
    检查是否为一次性请求体
    long
    获取内容长度
    abstract org.miaixz.bus.core.lang.MediaType
    获取媒体类型
    abstract void
    writeTo(org.miaixz.bus.core.io.sink.BufferSink sink)
    将请求体内容写入输出流

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RequestBody

      public RequestBody()
  • Method Details

    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType mediaType, String content)
      从字符串创建请求体

      如果 mediaType 非空且缺少字符集,则使用 UTF-8。

      Parameters:
      mediaType - 媒体类型(可能为 null)
      content - 内容字符串
      Returns:
      请求体实例
    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType mediaType, org.miaixz.bus.core.io.ByteString content)
      从 ByteString 创建请求体
      Parameters:
      mediaType - 媒体类型(可能为 null)
      content - 内容 ByteString
      Returns:
      请求体实例
    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType mediaType, byte[] content)
      从字节数组创建请求体
      Parameters:
      mediaType - 媒体类型(可能为 null)
      content - 内容字节数组
      Returns:
      请求体实例
      Throws:
      NullPointerException - 如果 content 为 null
    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType mediaType, byte[] content, int offset, int byteCount)
      从字节数组部分创建请求体
      Parameters:
      mediaType - 媒体类型(可能为 null)
      content - 内容字节数组
      offset - 偏移量
      byteCount - 字节数
      Returns:
      请求体实例
      Throws:
      NullPointerException - 如果 content 为 null
      ArrayIndexOutOfBoundsException - 如果 offset 或 byteCount 无效
    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType mediaType, File file)
      从文件创建请求体
      Parameters:
      mediaType - 媒体类型(可能为 null)
      file - 文件
      Returns:
      请求体实例
      Throws:
      NullPointerException - 如果 file 为 null
    • mediaType

      public abstract org.miaixz.bus.core.lang.MediaType mediaType()
      获取媒体类型
      Returns:
      媒体类型(可能为 null)
    • length

      public long length() throws IOException
      获取内容长度

      返回写入 sink 的字节数,如果未知则返回 -1。

      Returns:
      内容长度
      Throws:
      IOException - 如果无法确定长度
    • writeTo

      public abstract void writeTo(org.miaixz.bus.core.io.sink.BufferSink sink) throws IOException
      将请求体内容写入输出流
      Parameters:
      sink - 输出流
      Throws:
      IOException - 如果写入失败
    • isDuplex

      public boolean isDuplex()
      检查是否为双工请求体

      双工请求体允许请求和响应数据交错传输,仅支持 HTTP/2。 默认返回 false,除非子类重写。

      Returns:
      true 如果是双工请求体
    • isOneShot

      public boolean isOneShot()
      检查是否为一次性请求体

      一次性请求体只能传输一次,通常用于破坏性写入场景。 默认返回 false,除非子类重写。

      Returns:
      true 如果是一次性请求体