将 Base 64 String/Image 上传到 Azure 存储映像时出现空白 Spring 启动

Uploading Base 64 String/Image to Azure storage image is coming out blank Spring boot

我正在尝试将 base64 编码图像存储到 azure blob 存储。 PFB 的代码片段相同。 正在创建文件,但它以损坏的图像形式出现。

代码段

String mimeType = extractMimeType(imageBase64Encoded).replace("image/", "");
String fileName = "image." + mimeType;
BlobContainerClient containerClient = new BlobContainerClientBuilder()
                .connectionString(connectionString)
                .containerName(containerName)
                .buildClient();
BlobClient client = containerClient.getBlobClient(fileName);
String base64Data = imageBase64Encoded.split(",")[1];
byte[] base64DataBytes = base64Data.getBytes();
ByteArrayInputStream dataStream = new ByteArrayInputStream(base64DataBytes);
client.upload(dataStream, base64DataBytes.length);

您需要将 base64 编码的字符串解码为 byte[] 并使用该 byte[] 来存储图像。

String mimeType = extractMimeType(imageBase64Encoded).replace("image/", "");
String fileName = "image." + mimeType;
BlobContainerClient containerClient = new BlobContainerClientBuilder()
                .connectionString(connectionString)
                .containerName(containerName)
                .buildClient();
BlobClient client = containerClient.getBlobClient(fileName);
String base64Data = imageBase64Encoded.split(",")[1];
byte[] base64DataBytes = Base64.getDecoder().decode(base64Data);
ByteArrayInputStream dataStream = new ByteArrayInputStream(base64DataBytes);
client.upload(dataStream, base64DataBytes.length);