0
点赞
收藏
分享

微信扫一扫

七牛云存储____官方SDK集成开发


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 com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

/**
 * @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);
	}

	/**
	 * 七牛上传的方法
	 * 对同名的文件不处理 不覆盖
	 * @return
	 */
	public static void uploadFile(String bucket, String filePath, String key) {
		try {
			token = getToken(bucket);
			Response response = uploadManager.put(filePath, key, token);
			if(response.statusCode==200){
				System.out.println("图片上传成功...");
			}
		} catch (QiniuException e) {
			Response r = e.response;
			System.out.println(r.toString());
			e.printStackTrace();
		}
	}

	/**
	 * 覆盖上传
	 * 遇见同名的文件进行 覆盖
	 */
	public static void uploadCoverFile(String bucket, String filePath, String key) {
		try {
			token = auth.uploadToken(bucket, key, 6600, new StringMap().put("insertOnly", 0));
			Response response = uploadManager.put(filePath, key, token);
			if(response.statusCode==200){
				System.out.println("图片上传成功...");
			}
		} catch (QiniuException e) {
			Response r = e.response;
			System.out.println(r.toString());
			e.printStackTrace();
		}
	}

	/**
	 * 根据空间名和文件key 删除某空间的文件
	 * @param bucket
	 * @param key
	 */
	public static void delBucketKey(String bucket, String key) {
		try {
			bucketManager.delete(bucket, key);
		} catch (QiniuException e) {
			Response r = e.response;
			System.out.println(r.toString());
		}
	}

	

}

//测试


package com.scdn.qnscdn;

import java.util.UUID;

public class Test {
	public static void main(String[] args) {
		//确定自己的某个磁盘下有个图片
		String file="D:\\abc.jpg";
		QnUtil.uploadFile("test", file, UUID.randomUUID().toString());
		QnUtil.uploadCoverFile("test", file, UUID.randomUUID().toString());
	}
	
}



//测试结果

七牛云存储____官方SDK集成开发_云存储

//所用的jar包

七牛云存储____官方SDK集成开发_七牛云存储 cdn_02

举报

相关推荐

0 条评论