七牛云存储之Base64图片上传

  • 概述
    实际开发中,经常会遇到需要将图片转换为base64编码后的字符串进行处理,所以七牛云存储也提供了该接口的,支持base64图片上传。

  • 思路:

    1. 获取七牛提供的Auth对象,为获取token
    2. 指定需要上传的空间(bucket)和 文件存储在空间中的名词(key)
    3. 获取一个本地图片,并将其转换为base64字符串
    4. 通过http请求七牛提供的接口 put64
    5. 查看结果
  • 代码示例:

package com.qiniu.kodo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.codec.binary.Base64;

import com.qiniu.base.AccountMgr;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.UrlSafeBase64;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 图片用base64 上传至七牛云
 * @author xuhuanchao
 *
 */
public class UploadImgByBase64 {

    /**
     * main method
     * @param args
     */
    public static void main(String[] args) {
        String imgPath = "/Users/xuhuanchao/Downloads/sy1.jpg";
        uploadByBase64(imgPath);
    }

    /**
     * 获取token
     * @param auth
     * @param bucketName
     * @return
     */
    public static String getUploadToken(Auth auth, String bucketName) {
        String token = "";
        token = auth.uploadToken(bucketName);
        return token;
    }

    /**
     * upload base64-image to qiniu
     * @param imgPath
     */
    public static void uploadByBase64(String imgPath) {
        //修改一下ACCESS_KEY 和 SECRET_KEY
        Auth auth = Auth.create(AccountMgr.ACCESS_KEY, AccountMgr.SECRET_KEY);
        //自己修改一下空间名称 - bucketName 和 文件名称 - key
        String bucketName = "testspace";
        String token = getUploadToken(auth, bucketName);
        String key = "upload_by_base64_sy1.jpg";
        FileInputStream fis = null;
        try {
            File file = new File(imgPath);
            int fileSize = (int) file.length();
            fis = new FileInputStream(file);
            byte[] buff = new byte[fileSize];
            fis.read(buff);
            String fileStr = Base64.encodeBase64String(buff);
            String url = "http://upload.qiniu.com/putb64/" + fileSize +"/key/"+ UrlSafeBase64.encodeToString(key);
            RequestBody reqBody = RequestBody.create(null, fileStr);
            Request req = new Request.Builder()
                    .url(url)
                    .addHeader("Content-Type", "application/octet-stream")
                    .addHeader("Authorization", "UpToken " + token)
                    .post(reqBody)
                    .build();
            OkHttpClient client = new OkHttpClient();
            Response resp = client.newCall(req).execute();
            System.out.println(resp.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();//must close stream
            } catch (IOException e) {
                e.printStackTrace();
            }
        }   
    }
}

Tips: 当前使用的是 qiniu-java-sdk-7.2.1.jar ; 网络包是okhttp

  • 查看测试结果:
    如图所示:
    七牛云存储之Base64图片上传_第1张图片

附上七牛提供的上传base64图片的接口说明文档:
七牛云存储之Base64图片上传_第2张图片

你可能感兴趣的:(Qiniu)