Java 七牛云文件上传(本地)

3 篇文章 0 订阅
2 篇文章 0 订阅

引入pom.xml

<!-- https://mvnrepository.com/artifact/com.qiniu/qiniu-java-sdk -->
        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
            <version>7.2.0</version>
        </dependency>

配置文件QiNiuContant.java

/**
 * 七牛的配置文件
 * */
public class QiNiuContant {
    //每个七牛云仓库对应两对密钥
    public final static String AccessKey = "AccessKey ";
    public final static String SecretKey = "SecretKey ";

    public final static String __AccessKey__ = "AccessKey ";
    public final static String __SecretKey__  = "SecretKey ";

    public final static String bucketName = "bucketName";
}

主要QiNiuUtil.java

import com.education.ak1.contant.QiNiuContant;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

public class QiNiuUtil {

    public static String getUpToken(){
        Auth auth = Auth.create(QiNiuContant.AccessKey, QiNiuContant.SecretKey);
        String uptoken = auth.uploadToken("ak1ak1");
        return uptoken;
    }

    public static void  upload(String key, String localPath) {
        Configuration cfg = new Configuration(Zone.zone2());//设置华南的服务器
        UploadManager uploadManager = new UploadManager(cfg);

        String upToken = getUpToken();
        try {
            Response response = uploadManager.put(localPath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        }catch (QiniuException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception{
        upload("1234","E:\\VID_20190406_145318.mp4");
    }

}

特别注意:不同的区域选区的Zone.zone不同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自在如风。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值