七牛云存储____图片转Base64串上传

package com.scdn.qnscdn;

/**
 * 七牛常理配置
 * 
 * @author God 待整理好配到config文件里
 * ACCESSKEY 这是我个人申请的一个测试的号 everyone can useing it
 * SECRETKEY 
 */
public class QnConstant {
	// 设置好账号的ACCESS_KEY和SECRET_KEY
	public static final String ACCESSKEY = "1o90vmY9OmRcueIeIeFBID6q2uy8peFiFBxpnM78"; 
	// 账号里面可以找到
	public static final String SECRETKEY = "uapfWvebSlf6q8SebsJMrhzk4rudBciHQFIOnwj_";
	// 要上传的空间 对应要上传到七牛上 你的那个路径(自己建文件夹 注意设置公开)
	public static final String BUCKET_TEST = "test"; 
}



package com.scdn.qnscdn;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

/**
 * @author Administrator 七牛图片操作工具类
 */
public class QnUtil {
	// 授权验证
	public static Auth auth = Auth.create(QnConstant.ACCESSKEY, QnConstant.SECRETKEY);
	// 定义变量
	public static UploadManager uploadManager = null;
	public static BucketManager bucketManager = null;
	public static String accesskey = null;
	public static String secretkey = null;
	public static String bucket_test = null;
	// 令牌验证
	public static String token = null;
	// 初始化配置
	static {
		accesskey = QnConstant.ACCESSKEY;
		secretkey = QnConstant.SECRETKEY;
		bucket_test = QnConstant.BUCKET_TEST;
		uploadManager = new UploadManager();
		bucketManager = new BucketManager(auth);
	}

	/**
	 * 根据空间名获取token 获取test空间的token
	 * 
	 * @param bucket
	 * @return
	 */
	public static String getToken(String bucket) {
		return token = auth.uploadToken(bucket);
	}
	/**
	 * 七牛SDK_图片转base64串上传
	 * @param file
	 * @param key
	 * @throws Exception
	 */
	public static void putb64(String file,String key) throws Exception {
		FileInputStream fis = null;
		try {
			int l = (int) (new File(file).length());
			byte[] src = new byte[l];
			fis = new FileInputStream(new File(file));
			fis.read(src);
			String file64 = Base64.encodeBase64String(src);
			String url = "http://up.qiniu.com/putb64/-1/key/"+Base64.encodeBase64String((key+".jpg").getBytes());
			HttpPost post = new HttpPost(url);
			post.addHeader("Content-Type", "application/octet-stream");
			post.addHeader("Authorization", "UpToken " + QnUtil.getToken(bucket_test));
			post.setEntity(new StringEntity(file64));
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpResponse res = httpclient.execute(post);
			String responseBody = EntityUtils.toString(res.getEntity(), "UTF-8");
			System.out.println(responseBody);
			if (fis != null) {
				fis.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		String file="D:\\abc.jpg";
		try {
			putb64(file, UUID.randomUUID().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



//涉及的jar包



posted @ 2017-02-15 13:52  蜜獾互联网  阅读(4)  评论(0编辑  收藏  举报  来源