七牛云上传图片工具类

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

/**
 * 七牛云工具类
 */
public class QiniuUtils {
	// AK
    private static String accessKey = "*****************************";
    // SK
    private static String secretKey = "******************************";
    // 存储空间名
    private static String bucket = "zhr-health";

	// 上传文件为绝对路径
    public static boolean upload(String filePath, String fileName) {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region1());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(filePath, fileName, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            return response.isOK();
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
            return false;
        }
    }

	// 将上传文件转为字节码
    public static boolean upload(byte[] bytes, String fileName) {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region1());
        //...其他参数参考类注释

        UploadManager uploadManager = new UploadManager(cfg);

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);

        try {
            Response response = uploadManager.put(bytes, fileName, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            return response.isOK();
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
            return false;
        }
    }

	// 删除七牛云上的图片
    public static void delete(String fileName){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, fileName);
        } catch (QiniuException ex) {
            //如果遇到异常,说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
七牛云提供了丰富的工具和SDK,方便开发者在各种语言和平台上进行文件上传和下载操作。以下是七牛云实现上传下载图片的工具包: 1. Qiniu Java SDK:Java开发者可以使用Qiniu Java SDK来上传和下载文件,包括图片。该SDK提供了简单易用的API接口和丰富的功能,支持上传下载进度监听和断点续传等特性。 2. Qiniu Node.js SDK:Node.js开发者可以使用Qiniu Node.js SDK来上传和下载文件,包括图片。该SDK提供了简单易用的API接口和丰富的功能,支持上传下载进度监听和断点续传等特性。 3. Qiniu Python SDK:Python开发者可以使用Qiniu Python SDK来上传和下载文件,包括图片。该SDK提供了简单易用的API接口和丰富的功能,支持上传下载进度监听和断点续传等特性。 4. Qiniu PHP SDK:PHP开发者可以使用Qiniu PHP SDK来上传和下载文件,包括图片。该SDK提供了简单易用的API接口和丰富的功能,支持上传下载进度监听和断点续传等特性。 5. Qiniu Go SDK:Go开发者可以使用Qiniu Go SDK来上传和下载文件,包括图片。该SDK提供了简单易用的API接口和丰富的功能,支持上传下载进度监听和断点续传等特性。 以上是七牛云提供的一些常用语言的SDK,开发者可以根据需要选择相应的工具包来实现上传下载图片的功能。另外,七牛云还提供了丰富的文档和示例代码,方便开发者快速上手并完成开发任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值