base64上传图片到OSS

base64格式

base64图片或文件由以下格式组成:
第一部分: data:
第二部分: image/jpeg; (类型)
第三部分: base64, 
第四部分:base64内容

说明

base64图片上传到OOS需要先将读到的base64文件转化成字节,然后在进行上传。base64文件上传之前需要进行处理,否则上传图片无法正常使用。具体代码如下:

代码

package com.test.oss;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @Description: LocalTest
 * @Author: mingtian
 * @CreateDate: 2022/9/8 16:29
 * @Version: 1.0
 */
public class LocalTest {

    private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com";

    private static final String accessKeyId = "";
    private static final String accessKeySecret = "";
    private static final String bucketName = "test-head-portrait";

    private static final String base64_pre = "data:image/png;base64,";

    public static void main(String[] args) {
        // base64文件
        String base64ByCode = Objects.requireNonNull(getHeadPhotoBase64ByCode("1")).replace(base64_pre, "");
        // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
        String objectName = "images/2.png";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            // 填写Byte数组。
            byte[] content = new BASE64Decoder().decodeBuffer(base64ByCode);
            // 创建PutObject请求。
            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content));
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

/**
* 读取项目中配置的 base64图片,HP_1.txt
**/
private static String getHeadPhotoBase64ByCode(String headPhoto) { String imageBaseStr; try { ClassPathResource resource = new ClassPathResource("headPhoto/HP_" + headPhoto + ".txt"); if (!resource.exists()) { System.out.println("文件不存在"); return null; } imageBaseStr = IOUtils.toString(resource.getInputStream(), Objects.toString(StandardCharsets.UTF_8)); System.out.println("result:" + imageBaseStr); return imageBaseStr; } catch (IOException e) { System.out.println("错误信息:" + e); return null; } } }

 

posted @ 2022-09-08 17:02  明天,你好啊  阅读(1569)  评论(0编辑  收藏  举报