You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Java SDK上传文件至Azure Blob Storage时遇Base64解码错误求助

Azure Blob Storage Java SDK上传文件错误解决

核心错误原因

这个java.lang.IllegalArgumentException: Input byte array has wrong 4-byte ending unit异常,99%是Azure存储连接字符串格式不正确导致的——比如连接字符串中的AccountKey部分被截断、包含多余的空格/换行符,或是复制时遗漏了部分内容,导致Base64解码失败。

具体解决步骤

  • 检查并修正连接字符串
    确保连接字符串是从Azure门户完整复制的,标准格式为:

    DefaultEndpointsProtocol=https;AccountName=your_account_name;AccountKey=your_account_key;EndpointSuffix=core.windows.net
    

    重点确认AccountKey是完整的Base64字符串,没有多余空格或换行,很多人复制时会不小心带出行尾换行符,直接导致解码错误。

  • 修正代码变量名错误
    你代码中第一行创建的是BlobServiceClient client,但后续创建容器时误用了blobServiceClient.createBlobContainer,变量名不匹配会导致编译/运行错误,应改为client.createBlobContainer(containerName)。

  • 处理容器已存在的情况
    如果目标容器已经存在,createBlobContainer会抛出异常,建议改用createBlobContainerIfNotExists自动创建(不存在时),或直接用getBlobContainerClient获取已有容器。

  • 优化文件操作逻辑
    使用try-with-resources语法自动关闭文件流,避免资源泄漏;同时上传前检查本地文件是否创建成功:

    // 创建本地文件的优化写法
    try (FileWriter writer = new FileWriter(localPath + fileName, true)) {
        writer.write("Hello, World!");
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
        return;
    }
    
    // 上传前验证文件存在性
    File uploadFile = new File(localPath + fileName);
    if (!uploadFile.exists()) {
        System.out.println("本地文件不存在:" + uploadFile.getAbsolutePath());
        return;
    }
    blobClient.uploadFromFile(uploadFile.getAbsolutePath());
    

修正后的完整代码示例

import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class BlobUploadDemo {
    public static void main(String[] args) {
        String connectStr = "你的Azure存储连接字符串";
        String containerName = "目标容器名称";
        String localPath = "./data/";
        String fileName = "quickstart" + java.util.UUID.randomUUID() + ".txt";

        // 初始化Blob服务客户端
        BlobServiceClient client = new BlobServiceClientBuilder()
                .connectionString(connectStr)
                .buildClient();

        // 获取容器(不存在则自动创建)
        BlobContainerClient blobContainerClient = client.getBlobContainerClient(containerName);
        blobContainerClient.createIfNotExists();

        // 创建本地测试文件
        try (FileWriter writer = new FileWriter(localPath + fileName, true)) {
            writer.write("Hello, World!");
        } catch (IOException ex) {
            System.out.println("文件创建失败:" + ex.getMessage());
            return;
        }

        // 上传文件到Blob存储
        BlobClient blobClient = blobContainerClient.getBlobClient(fileName);
        File uploadFile = new File(localPath + fileName);
        if (uploadFile.exists()) {
            blobClient.uploadFromFile(uploadFile.getAbsolutePath());
            System.out.println("文件上传成功,访问地址:" + blobClient.getBlobUrl());
        } else {
            System.out.println("无法找到本地文件:" + uploadFile.getAbsolutePath());
        }
    }
}

内容的提问来源于stack exchange,提问作者Sensei sai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 20:01:06