Class OpenMimeType
java.lang.Object
cloud.opencode.base.io.OpenMimeType
MIME Type Detection Utility Class
MIME 类型检测工具类
Provides MIME type detection from file extension and content (magic numbers).
提供基于文件扩展名和内容(魔数)的 MIME 类型检测。
Features | 主要功能:
- Detect by file extension - 按扩展名检测
- Detect by content (magic numbers) - 按内容检测(魔数)
- Comprehensive MIME type mappings - 完整的 MIME 类型映射
Usage Examples | 使用示例:
// Detect by extension
String mime = OpenMimeType.fromExtension("pdf"); // "application/pdf"
// Detect from file (extension + content)
String mime = OpenMimeType.detect(Path.of("image.png"));
// Detect from content only
String mime = OpenMimeType.fromContent(bytes);
Security | 安全性:
- Thread-safe: Yes (stateless utility with static maps) - 线程安全: 是(使用静态映射的无状态工具类)
- Null-safe: Yes, null/unknown returns application/octet-stream - 空值安全: 是,null/未知返回application/octet-stream
- Since:
- JDK 25, opencode-base-io V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringCommon JSON MIME type 常见 JSON MIME 类型static final StringDefault MIME type for unknown content 未知内容的默认 MIME 类型static final StringCommon PDF MIME type 常见 PDF MIME 类型static final StringCommon XML MIME type 常见 XML MIME 类型static final StringCommon HTML MIME type 常见 HTML MIME 类型static final StringCommon text MIME type 常见文本 MIME 类型 -
Method Summary
Modifier and TypeMethodDescriptionstatic StringDetect MIME type from file (combines extension and content detection) 从文件检测 MIME 类型(综合扩展名和内容检测)static StringdetectByExtension(Path path) Detect MIME type preferring extension over content 优先使用扩展名检测 MIME 类型fromContent(byte[] data) Detect MIME type from byte array content 从字节数组内容检测 MIME 类型fromContent(InputStream input) Detect MIME type from input stream 从输入流检测 MIME 类型fromContent(Path path) Detect MIME type from file content (magic numbers) 从文件内容检测 MIME 类型(魔数)fromExtension(String extension) Get MIME type from file extension 从文件扩展名获取 MIME 类型static StringfromExtension(String extension, String defaultMime) Get MIME type from file extension with default 从文件扩展名获取 MIME 类型(带默认值)fromFilename(String filename) Get MIME type from filename 从文件名获取 MIME 类型Get MIME type from path extension 从路径扩展名获取 MIME 类型getExtension(String mimeType) Get file extension for MIME type 获取 MIME 类型对应的文件扩展名static booleanCheck if MIME type is audio 检查 MIME 类型是否为音频static booleanCheck if MIME type is binary 检查 MIME 类型是否为二进制static booleanCheck if MIME type is image 检查 MIME 类型是否为图片static booleanCheck if MIME type is text-based 检查 MIME 类型是否为文本类型static booleanCheck if MIME type is video 检查 MIME 类型是否为视频
-
Field Details
-
APPLICATION_OCTET_STREAM
Default MIME type for unknown content 未知内容的默认 MIME 类型- See Also:
-
TEXT_PLAIN
-
TEXT_HTML
-
APPLICATION_JSON
-
APPLICATION_XML
-
APPLICATION_PDF
-
-
Method Details
-
fromExtension
-
fromExtension
-
fromPath
-
fromFilename
-
fromContent
-
fromContent
-
fromContent
Detect MIME type from input stream 从输入流检测 MIME 类型- Parameters:
input- the input stream (must support mark/reset) | 输入流(必须支持 mark/reset)- Returns:
- MIME type or empty if unknown | MIME 类型,未知时返回空
-
detect
Detect MIME type from file (combines extension and content detection) 从文件检测 MIME 类型(综合扩展名和内容检测)Priority: content detection > extension detection > system probe
优先级:内容检测 > 扩展名检测 > 系统探测
- Parameters:
path- the file path | 文件路径- Returns:
- MIME type, defaults to application/octet-stream | MIME 类型,默认 application/octet-stream
-
detectByExtension
-
isText
Check if MIME type is text-based 检查 MIME 类型是否为文本类型- Parameters:
mimeType- the MIME type | MIME 类型- Returns:
- true if text-based | 如果是文本类型返回 true
-
isImage
Check if MIME type is image 检查 MIME 类型是否为图片- Parameters:
mimeType- the MIME type | MIME 类型- Returns:
- true if image | 如果是图片返回 true
-
isAudio
Check if MIME type is audio 检查 MIME 类型是否为音频- Parameters:
mimeType- the MIME type | MIME 类型- Returns:
- true if audio | 如果是音频返回 true
-
isVideo
Check if MIME type is video 检查 MIME 类型是否为视频- Parameters:
mimeType- the MIME type | MIME 类型- Returns:
- true if video | 如果是视频返回 true
-
isBinary
Check if MIME type is binary 检查 MIME 类型是否为二进制- Parameters:
mimeType- the MIME type | MIME 类型- Returns:
- true if binary (not text) | 如果是二进制返回 true
-
getExtension
-