Class ContentType

java.lang.Object
cloud.opencode.base.web.http.ContentType

public final class ContentType extends Object
Content Type - HTTP Content-Type Header Values 内容类型 - HTTP Content-Type 头部值

This class provides common MIME types and utilities for Content-Type handling.

此类提供常见的 MIME 类型和 Content-Type 处理工具。

Example | 示例:

String json = ContentType.APPLICATION_JSON;
ContentType type = ContentType.parse("text/html; charset=utf-8");
String mimeType = type.getMimeType();
Charset charset = type.getCharset();

Features | 主要功能:

  • Common MIME type constants - 常见MIME类型常量
  • Content-Type header parsing - Content-Type头部解析
  • Charset and boundary support - 字符集和边界支持
  • Type checking methods (isJson, isXml, etc.) - 类型检查方法

Usage Examples | 使用示例:

ContentType json = ContentType.json();
ContentType parsed = ContentType.parse("text/html; charset=utf-8");
boolean isJson = parsed.isJson();

Security | 安全性:

  • Thread-safe: Yes (immutable) - 是(不可变)
  • Null-safe: Partial (parse returns null for null input) - 部分(parse对null输入返回null)
Since:
JDK 25, opencode-base-web V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

  • Method Details

    • of

      public static ContentType of(String mimeType)
      Creates a ContentType with MIME type only. 仅使用 MIME 类型创建 ContentType。
      Parameters:
      mimeType - the MIME type - MIME 类型
      Returns:
      the ContentType - ContentType
    • of

      public static ContentType of(String mimeType, Charset charset)
      Creates a ContentType with MIME type and charset. 使用 MIME 类型和字符集创建 ContentType。
      Parameters:
      mimeType - the MIME type - MIME 类型
      charset - the charset - 字符集
      Returns:
      the ContentType - ContentType
    • json

      public static ContentType json()
      Creates a ContentType for JSON with UTF-8. 创建 UTF-8 编码的 JSON ContentType。
      Returns:
      the ContentType - ContentType
    • xml

      public static ContentType xml()
      Creates a ContentType for XML with UTF-8. 创建 UTF-8 编码的 XML ContentType。
      Returns:
      the ContentType - ContentType
    • form

      public static ContentType form()
      Creates a ContentType for form data. 创建表单数据的 ContentType。
      Returns:
      the ContentType - ContentType
    • multipart

      public static ContentType multipart(String boundary)
      Creates a ContentType for multipart with boundary. 使用边界创建 multipart 的 ContentType。
      Parameters:
      boundary - the boundary - 边界
      Returns:
      the ContentType - ContentType
    • text

      public static ContentType text()
      Creates a ContentType for plain text with UTF-8. 创建 UTF-8 编码的纯文本 ContentType。
      Returns:
      the ContentType - ContentType
    • binary

      public static ContentType binary()
      Creates a ContentType for binary data. 创建二进制数据的 ContentType。
      Returns:
      the ContentType - ContentType
    • parse

      public static ContentType parse(String value)
      Parses a Content-Type header value. 解析 Content-Type 头部值。
      Parameters:
      value - the header value - 头部值
      Returns:
      the ContentType - ContentType
    • getMimeType

      public String getMimeType()
      Gets the MIME type. 获取 MIME 类型。
      Returns:
      the MIME type - MIME 类型
    • getCharset

      public Charset getCharset()
      Gets the charset. 获取字符集。
      Returns:
      the charset or null - 字符集或 null
    • getCharsetOrDefault

      public Charset getCharsetOrDefault(Charset defaultCharset)
      Gets the charset or default. 获取字符集或默认值。
      Parameters:
      defaultCharset - the default charset - 默认字符集
      Returns:
      the charset - 字符集
    • getBoundary

      public String getBoundary()
      Gets the boundary for multipart. 获取 multipart 的边界。
      Returns:
      the boundary or null - 边界或 null
    • isJson

      public boolean isJson()
      Checks if this is a JSON type. 检查是否是 JSON 类型。
      Returns:
      true if JSON - 如果是 JSON 返回 true
    • isXml

      public boolean isXml()
      Checks if this is an XML type. 检查是否是 XML 类型。
      Returns:
      true if XML - 如果是 XML 返回 true
    • isText

      public boolean isText()
      Checks if this is a text type. 检查是否是文本类型。
      Returns:
      true if text - 如果是文本返回 true
    • isMultipart

      public boolean isMultipart()
      Checks if this is a multipart type. 检查是否是 multipart 类型。
      Returns:
      true if multipart - 如果是 multipart 返回 true
    • isForm

      public boolean isForm()
      Checks if this is a form type. 检查是否是表单类型。
      Returns:
      true if form - 如果是表单返回 true
    • toString

      public String toString()
      Returns the Content-Type header value. 返回 Content-Type 头部值。
      Overrides:
      toString in class Object
      Returns:
      the header value - 头部值