MediaUtils
public class MediaUtils
io/agentscope/core/formatter/MediaUtils.java:44–449Media Utils,在智能体、模型与前端之间传递结构化消息和事件。位于 io.agentscope.core.formatter 包。核心成员包括 isLocalFile、fileToBase64、downloadUrlToBase64、urlToProtocolUrl、toFileProtocolUrl、urlToInputStream。
Utility class for handling media files in multimodal content. Provides methods for processing images, audio, and other media types.
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public static boolean isLocalFile(String url)static | 判断是否本地文件。 Check if a URL is a local file path (not a URL with protocol scheme). |
public static String fileToBase64(String path) throws IOExceptionstatic | 设置文件到基类 64。 Convert a local file to base64 encoded string. |
public static String downloadUrlToBase64(String url) throws IOExceptionstatic | 设置下载 URL 到基类 64。 Download a remote URL and convert to base64. |
public static String urlToProtocolUrl(String url) throws IOExceptionstatic | 设置URL 到协议 URL。 Convert a URL to a file:// protocol URL or leave as-is for web URLs. |
public static String toFileProtocolUrl(String path) throws IOExceptionstatic | 转换本地文件路径到文件://协议 URL。 Convert a local file path to file:// protocol URL. |
public static InputStream urlToInputStream(String url) throws IOExceptionstatic | 设置URL 到输入流。 Convert a file or web URL to an InputStream. |
public static InputStream urlToRgbaImageInputStream(String url) throws IOExceptionstatic | 设置该参数。 Convert a file or web URL to an InputStream with RGBA image. |
public static String urlToBase64DataUrl(String url) throws IOExceptionstatic | 设置URL 到基类 64 数据 URL。 Convert a file or web URL to a data URL with base64 encoding. |
public static String determineMediaType(String path)static | 设置该参数。 Determine MIME type from file extension. |
public static String resolveMimeType(Source source)static | 解析MIME 类型。 Resolve the MIME type from a `Source`. |
public static void validateImageExtension(String url)static | 校验图像扩展。 Validate that an image file has a supported extension. |
public static void validateAudioExtension(String url)static | 校验音频扩展。 Validate that an audio file has a supported extension. |
public static void validateVideoExtension(String url)static | 校验视频扩展。 Validate that a video file has a supported extension. |
public static String determineAudioFormat(String path)static | 设置该参数。 Determine audio format string from file extension. |
public static String inferAudioFormatFromMediaType(String mediaType)static | 设置该参数。 Infer audio format string from MIME type. |
public static String getExtension(String path)static | 获取扩展。 Extract file extension from path or URL. |
public static boolean isFileExists(String path)static | 判断是否 `isFileExists`。 Check if a file exists. |
方法详情Method Detail
isLocalFile
public static boolean isLocalFile(String url)
判断是否本地文件。
Check if a URL is a local file path (not a URL with protocol scheme).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
url | String | URL 或文件路径到检查。The URL or file path to check |
booleanio/agentscope/core/formatter/MediaUtils.java:71–80fileToBase64
public static String fileToBase64(String path) throws IOException
设置文件到基类 64。
Convert a local file to base64 encoded string.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | String | 本地文件路径。The local file path |
StringIOException可能抛出 `IOException`。Thrown when the operation cannot be completed.
io/agentscope/core/formatter/MediaUtils.java:91–102downloadUrlToBase64
public static String downloadUrlToBase64(String url) throws IOException
设置下载 URL 到基类 64。
Download a remote URL and convert to base64.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
url | String | 远程 URL 到下载。The remote URL to download |
StringIOException可能抛出 `IOException`。Thrown when the operation cannot be completed.
io/agentscope/core/formatter/MediaUtils.java:113–150urlToProtocolUrl
public static String urlToProtocolUrl(String url) throws IOException
设置URL 到协议 URL。
Convert a URL to a file:// protocol URL or leave as-is for web URLs.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
url | String | 服务地址(类型 `String`)。URL or file path |
StringIOException可能抛出 `IOException`。Thrown when the operation cannot be completed.
io/agentscope/core/formatter/MediaUtils.java:159–165toFileProtocolUrl
public static String toFileProtocolUrl(String path) throws IOException
转换本地文件路径到文件://协议 URL。
Convert a local file path to file:// protocol URL.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | String | 路径(类型 `String`)。Local file path (relative or absolute) |
StringIOException可能抛出 `IOException`。Thrown when the operation cannot be completed.
io/agentscope/core/formatter/MediaUtils.java:176–182urlToInputStream
public static InputStream urlToInputStream(String url) throws IOException
设置URL 到输入流。
Convert a file or web URL to an InputStream.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
url | String | 服务地址(类型 `String`)。The file or web URL |
InputStreamIOException可能抛出 `IOException`。Thrown when the operation cannot be completed.
io/agentscope/core/formatter/MediaUtils.java:191–199urlToRgbaImageInputStream
public static InputStream urlToRgbaImageInputStream(String url) throws IOException
设置该参数。
Convert a file or web URL to an InputStream with RGBA image.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
url | String | 服务地址(类型 `String`)。a local file path or web URL |
InputStreamIOException可能抛出 `IOException`。Thrown when the operation cannot be completed.
io/agentscope/core/formatter/MediaUtils.java:208–226urlToBase64DataUrl
public static String urlToBase64DataUrl(String url) throws IOException
设置URL 到基类 64 数据 URL。
Convert a file or web URL to a data URL with base64 encoding.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
url | String | 服务地址(类型 `String`)。a local file path or web url |
StringIOException可能抛出 `IOException`。Thrown when the operation cannot be completed.
io/agentscope/core/formatter/MediaUtils.java:233–245determineMediaType
public static String determineMediaType(String path)
设置该参数。
Determine MIME type from file extension.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | String | 路径(类型 `String`)。The path. |
Stringio/agentscope/core/formatter/MediaUtils.java:250–281resolveMimeType
public static String resolveMimeType(Source source)
解析MIME 类型。
Resolve the MIME type from a Source.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
source | Source | 来源到解析 MIME 类型来自。the source to resolve MIME type from |
StringIllegalArgumentException可能抛出 `IllegalArgumentException`。if the type cannot be determined or source type is unknown
io/agentscope/core/formatter/MediaUtils.java:297–316validateImageExtension
public static void validateImageExtension(String url)
校验图像扩展。
Validate that an image file has a supported extension.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
url | String | 服务地址(类型 `String`)。The url. |
io/agentscope/core/formatter/MediaUtils.java:321–328validateAudioExtension
public static void validateAudioExtension(String url)
校验音频扩展。
Validate that an audio file has a supported extension.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
url | String | 服务地址(类型 `String`)。The url. |
io/agentscope/core/formatter/MediaUtils.java:333–341validateVideoExtension
public static void validateVideoExtension(String url)
校验视频扩展。
Validate that a video file has a supported extension.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
url | String | 服务地址(类型 `String`)。The url. |
io/agentscope/core/formatter/MediaUtils.java:346–354determineAudioFormat
public static String determineAudioFormat(String path)
设置该参数。
Determine audio format string from file extension.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | String | 文件路径。The file path |
Stringio/agentscope/core/formatter/MediaUtils.java:362–365inferAudioFormatFromMediaType
public static String inferAudioFormatFromMediaType(String mediaType)
设置该参数。
Infer audio format string from MIME type.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
mediaType | String | MIME 类型。The MIME type |
Stringio/agentscope/core/formatter/MediaUtils.java:373–385getExtension
public static String getExtension(String path)
获取扩展。
Extract file extension from path or URL.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | String | 路径(类型 `String`)。The path. |
Stringio/agentscope/core/formatter/MediaUtils.java:390–418isFileExists
public static boolean isFileExists(String path)
判断是否 isFileExists。
Check if a file exists.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | String | 一种本地文件路径或 URL。a local file path or URL. |
booleanio/agentscope/core/formatter/MediaUtils.java:425–434