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不同

好的,以下是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 的相关依赖包。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

自在如风。

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

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

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

打赏作者

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

抵扣说明:

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

余额充值