📜  上传 base64 图像 - 任何代码示例

📅  最后修改于: 2022-03-11 15:00:44.809000             🧑  作者: Mango

代码示例1
// Create a source file client
ShareFileClient dirClient = new ShareFileClientBuilder()
    .endpoint("https://xxx.file.core.windows.net")
    .shareName("xxx")
    .credential(new StorageSharedKeyCredential("xxx", "xxx"))
    .resourcePath("photos" + "/" + "test.png")
    .buildFileClient();

var base64Image = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";

// Create a source file
try {
    var sizeInBytes = 4 * Math.ceil(base64Image.length() / 3)*0.5624896334383812;
    dirClient.create((long) sizeInBytes);
} catch (ShareStorageException e) {
    System.out.println("Failed to create source client. Reasons: " + e.getMessage());
}

// decodeBase64
byte[] bytes = Base64.decodeBase64(base64Image);

// Upload
try (ByteArrayInputStream dataStream = new ByteArrayInputStream(bytes)) {
    dirClient.upload(dataStream, bytes.length);
} catch (IOException e) {
    e.printStackTrace();
}