Java七牛云报400错误

Java七牛云报400错误

报错:{ResponseInfo:com.qiniu.http.Response@29a28d60,status:400, reqId:HioAAG-QCb69S40V, xlog:body;UP/400, xvia:, adress:up.qiniu.com/115.231.97.46:80, duration:0.000000 s, error:incorrect region, please use up-z2.qiniup.com}
1.添加maven依赖

<dependency>
				<groupId>com.qiniu</groupId>
				<artifactId>qiniu-java-sdk</artifactId>
				<version>7.2.10</version>
			</dependency>

2.ImgUploadResult对象

import java.math.BigDecimal;

public class ImgUploadResult {

    /**
     * 是否上传成功
     */
    private boolean success;

    /**
     * 图片地址
     */
    private String url;

    /**
     * 图片尺寸,单位byte
     */
    private BigDecimal size;

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public BigDecimal getSize() {
        return size;
    }

    public void setSize(BigDecimal size) {
        this.size = size;
    }

}

3.QiNiuApi对象

import java.math.BigDecimal;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hwx.framework.util.DateUtil;
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.util.Auth;

public class QiNiuApi {

    private static Logger logger = LoggerFactory.getLogger(QiNiuApi.class);

    private String accessKey;

    private String secretKey;

    private String token;

    private UploadManager uploadManager;
    // 要上传的空间
    private String bucketname;

    /**
     * token过期时间
     */
    private Date expiredTime;

    // 上传之后保存文件url的前缀,前缀+upload方法的fileName参数可以构成一个可访问的文件url
    private String urlPrefix;

    public QiNiuApi(String accessKey, String secretKey, String bucketName, String urlPrefix) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.bucketname = bucketName;
        this.urlPrefix = urlPrefix;

        Auth auth = Auth.create(this.accessKey, this.secretKey);
        this.token = auth.uploadToken(this.bucketname);
        this.expiredTime = DateUtil.addMinutes(new Date(), 40);
        Zone z = Zone.autoZone();
        Configuration c = new Configuration(z);
        this.uploadManager = new UploadManager(c);
    }

    public ImgUploadResult upload(byte[] data, String fileName) {
        ImgUploadResult result = new ImgUploadResult();
        result.setSuccess(true);
        if (null == data) {
            result.setSize(BigDecimal.ZERO);
            logger.error("上传到七牛的文件内容为Null,fileName{}.", fileName);

            return result;
        }
        try {
            // 调用put方法上传
            Response res = uploadManager.put(data, fileName, token);
            // 打印返回的信息
            if (null != res && res.isOK()) {
                result.setUrl(urlPrefix + fileName);
                result.setSize(new BigDecimal(data.length));
            } else {
                logger.error("上传文件到七牛失败,reponse={},response.error={}.,", null != res ? res : "res为null",
                        null != res ? res.error : "");
                result.setSuccess(false);
                result.setSize(BigDecimal.ZERO);
            }
        } catch (QiniuException e) {
            Response r = e.response;
            try {
                logger.error("上传文件到七牛失败,reponse={}:", r.bodyString(), e);
            } catch (QiniuException e1) {
                logger.error("上传文件到七牛失败:", e);
            }
            result.setSuccess(false);
            result.setSize(BigDecimal.ZERO);
        } catch (Exception e) {
            logger.error("上传文件到七牛失败:", e);
            result.setSuccess(false);
            result.setSize(BigDecimal.ZERO);
        }

        return result;
    }

    public Date getExpiredTime() {
        return expiredTime;
    }

    public void setExpiredTime(Date expiredTime) {
        this.expiredTime = expiredTime;
    }

    /**
     * token是否已过期
     * 
     * @return
     */
    public boolean isExpired() {
        return new Date().compareTo(expiredTime) > 0;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getBucketname() {
        return bucketname;
    }

    public void setBucketname(String bucketname) {
        this.bucketname = bucketname;
    }

    public String getUrlPrefix() {
        return urlPrefix;
    }

    public void setUrlPrefix(String urlPrefix) {
        this.urlPrefix = urlPrefix;
    }

}

4.QiNiuFactory类

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.hwx.framework.base.exception.ServiceException;

public class QiNiuFactory {

    private static Map<String, QiNiuApi> qiNiuApiContainer = new HashMap<String, QiNiuApi>();

    private static final String accessKey_KEY = "accessKey";

    private static final String secretKey_KEY = "secretKey";

    private static final String bucketName_KEY = "bucketName";

    private static final String urlPrefix_KEY = "urlPrefix";

    public static QiNiuApi getQiNiuApi(String qiNiuParam) {

        if (StringUtils.isBlank(qiNiuParam)) {
            throw new ServiceException("", "图片服务器参数无效");
        }

        JSONObject json = JSONObject.parseObject(qiNiuParam);

        String accessKey = json.getString(accessKey_KEY);

        String secretKey = json.getString(secretKey_KEY);

        String bucketName = json.getString(bucketName_KEY);

        String urlPrefix = json.getString(urlPrefix_KEY);

        if (StringUtils.isBlank(accessKey) || StringUtils.isBlank(secretKey) || StringUtils.isBlank(bucketName)
                || StringUtils.isBlank(urlPrefix)) {
            throw new ServiceException("", "图片服务器参数无效");
        }
        if (null == qiNiuApiContainer.get(qiNiuParam)) {
            QiNiuApi qiNiuApi = new QiNiuApi(accessKey, secretKey, bucketName, urlPrefix);
            qiNiuApiContainer.put(qiNiuParam, qiNiuApi);

            return qiNiuApi;
        } else {
            QiNiuApi qiNiuApi = qiNiuApiContainer.get(qiNiuParam);
            if (qiNiuApi.isExpired()) {
                qiNiuApi = new QiNiuApi(accessKey, secretKey, bucketName, urlPrefix);
                qiNiuApiContainer.put(qiNiuParam, qiNiuApi);
            }
            return qiNiuApi;
        }

    }

}

5.把这边这个三类复制到自己的本地,引用自己本地包

GitHub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值