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.csdn.net/qq_33924360/article/details/89153493

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java代码实现七牛云上传本地图片到指定文件夹,并自定义文件名的示例: ```java import com.qiniu.storage.Configuration; import com.qiniu.storage.UploadManager; import com.qiniu.util.Auth; import java.io.File; public class QiniuUploader { // 七牛云配置信息 private static final String ACCESS_KEY = "your_access_key_here"; private static final String SECRET_KEY = "your_secret_key_here"; private static final String BUCKET_NAME = "your_bucket_name_here"; // 上传文件到指定文件夹,并自定义文件名 public static void uploadFile(String filePath, String folder, String fileName) { // 构造一个带指定Zone对象的配置类 Configuration cfg = new Configuration(Zone.autoZone()); // 创建上传管理器 UploadManager uploadManager = new UploadManager(cfg); // 生成上传凭证,然后准备上传 Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); String upToken = auth.uploadToken(BUCKET_NAME); try { // 上传文件 uploadManager.put(filePath, folder + "/" + fileName, upToken); // 打印上传结果 System.out.println("上传成功"); } catch (Exception ex) { ex.printStackTrace(); System.out.println("上传失败"); } } public static void main(String[] args) { // 测试上传 String filePath = "your_local_file_path_here"; String folder = "your_destination_folder_here"; String fileName = "your_custom_file_name_here"; uploadFile(filePath, folder, fileName); } } ``` 需要注意的是,你需要将代码中的 `your_access_key_here`、`your_secret_key_here` 和 `your_bucket_name_here` 替换为你自己的七牛云配置信息,将 `your_local_file_path_here`、`your_destination_folder_here` 和 `your_custom_file_name_here` 替换为你自己的本地文件路径、目标文件夹和自定义文件名。另外,你需要在代码中引入七牛云 SDK 的相关依赖包。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值