java实现七牛云上传

1.代码

package com.mindskip.xzs;

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;
 
/**
 * 七牛 java sdk 简单上传,覆盖上传,文件上传
 * @author yunlingfly
 */
public class SimpleUpload {


    public static void main(String[] args) {
        String accessKey = "xxx";
        String secretKey = "xxx";
        String bucket = "xzs123";

        Configuration configuration = new Configuration(Zone.zone2());
        UploadManager manager = new UploadManager(configuration);

        String key = "xzs-mysql.sql";

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        String localFilePath = "C:\\Users\\ASUS\\Desktop\\xzs-mysql.sql";


        Response response = null;
        try {
            response = manager.put(localFilePath, key, upToken);
            DefaultPutRet set = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(set.key);
            System.out.println(set.hash);

        } catch (QiniuException e) {
            Response r = e.response;
            System.err.println(r.toString());
        }
    }

}

2.有报错
*info ===== <QNResponseInfo= id: CB441DB1-B50D-472A-8DAB-B07700E102C5, ver: 7.1.6, status: 400, requestId:XXXX, XXXX, xlog: RA:122.224.95.105:80;UP/400;NUPROXY:28/400, xvia: (null), host: upload.qiniu.com ip: 180.97.72.163 duration: 0.068638 s time: 1503737206 error: Error Domain=qiniu.com Code=400 “(null)” UserInfo={error=incorrect region, please use up-z2.qiniu.com}>*

问题原因
创建七牛云存储空间时选择了华南地区,但上传文件的配置类中配置了其他地区

解决办法:
在配置Configuration类的时候,按需选择Zone的类型即可

//Zone.zone0:华东 
//Zone.zone1:华北 
//Zone.zone2:华南 
//Zone.zoneNa0:北美 
//———http上传,自动识别上传区域—— 
//Zone.httpAutoZone 
//———https上传,自动识别上传区域—— //Zone.httpsAutoZone

//指定上传文件服务器地址:
Configuration cfg = new Configuration(Zone.zone2());

详细教程springboot上传图片可参考:https://blog.youkuaiyun.com/qq_33924360/article/details/89153493

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值