文件Base64处理

使用阿里云视觉智能开放平台服务时,部分能力支持传入经过Base64编码处理的文件,视觉智能平台对传入的Base64文件进行解码之后还原文件,进行后续智能处理。

说明

阿里云视觉智能开放平台各类目视觉AI能力API接入、接口使用或问题咨询等,请通过钉钉群(23109592)加入阿里云视觉智能开放平台咨询群联系我们。

测试样例

为了保证您处理之后的Base64内容是正确的,我们为您提供测试样例base64Test

您可以下载该样例文件并进行Base64编码处理,处理之后的正确结果为:

iVBORw0KGgoAAAANSUhEUgAAAC8AAAA4CAYAAABt9KGPAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAhGVYSWZNTQAqAAAACAAFARIAAwAAAAEAAQAAARoABQAAAAEAAABKARsABQAAAAEAAABSASgAAwAAAAEAAgAAh2kABAAAAAEAAABaAAAAAAAAAEgAAAABAAAASAAAAAEAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAL6ADAAQAAAABAAAAOAAAAADJruBmAAAACXBIWXMAAAsTAAALEwEAmpwYAAACMmlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iWE1QIENvcmUgNi4wLjAiPgogICA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczpleGlmPSJodHRwOi8vbnMuYWRvYmUuY29tL2V4aWYvMS4wLyIKICAgICAgICAgICAgeG1sbnM6dGlmZj0iaHR0cDovL25zLmFkb2JlLmNvbS90aWZmLzEuMC8iPgogICAgICAgICA8ZXhpZjpQaXhlbFlEaW1lbnNpb24+NTYwPC9leGlmOlBpeGVsWURpbWVuc2lvbj4KICAgICAgICAgPGV4aWY6UGl4ZWx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k0lkGPzKdMcibckZPMzQVPmDk1O8ikEGBbhGkykpRzDAxKUv7GQpht6L5qmAy6j3bf8LGugva+GXvswAAAAASUVORK5CYII=

工具处理

您可以自行检索并使用Base64处理工具,或者安装命令行工具进行处理。

此处以Mac电脑环境为例进行说明:

  1. 通过Homebrew安装base64工具,更多信息,请参见Homebrew

    brew install base64
  2. 通过base64工具对文件进行编码。

    base64Test.png是您的文件名。

    base64 base64Test.png

代码处理

您也可以使用代码进行文件Base64的处理,此处以Java代码为例进行说明。

Java代码示例:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class Main {

    public static String encryptToBase64(String filePath) {
        if (filePath == null) {
            return null;
        }
        try {
            byte[] b = Files.readAllBytes(Paths.get(filePath));
            return Base64.getEncoder().encodeToString(b);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args) {
        System.out.println(encryptToBase64("/tmp/base64Test.png"));
    }

}
说明

本示例需要Java 8及以上环境。

常见问题

如果有类似data:image/png;base64,xxxx格式如何处理?

如果您获取到的Base64字符串类似data:image/png;base64,xxxx这种格式,在传入API时,不需要前面的data:image/png;base64,部分,只需要传后面的xxxx开始的部分即可。