开发者导航

作者:开发者导航 网址:www.codernav.com 简介:开发者常用的网址集锦。

博客园 首页 新随笔 联系 订阅 管理

原文摘自:https://www.codernav.com

第一步:新建springboot项目,引入jar包,其中hutool-all是工具类,用来写文件下载,可以随意更换。

<!--工具类-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.2.0</version>
        </dependency>
        <!--七牛云相关jar包-->
        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
            <version>[7.2.0, 7.2.99]</version>
        </dependency>

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.14.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>happy-dns-java</artifactId>
            <version>0.1.6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

第二步:登录七牛云,在秘钥管理中找到accessKey和secretKey

 

 

第三步:写测试类,代码结构如下:

 

 

package com.example.demo;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.StreamProgress;
import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpUtil;
import com.qiniu.common.Zone;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 架构师小跟班 www.codernav.com
 * 官方API说明:https://developer.qiniu.com/kodo/sdk/1239/java#1
 */
@SpringBootApplication
public class DemoApplication {
    //七牛云秘钥AK
    static String accessKey = "xxxxxxxxxxxxxxxxxxxxxxx";
    //七牛云秘钥SK
    static String secretKey = "xxxxxxxxxxxxxxxxxxxxxxx";
    //七牛云空间名称
    static String bucket = "xxxxxx";

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        list();
    }

    /**
     * 列表
     */
    public static void list() {
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Region.region0());
        //...其他参数参考类注释
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);

        //文件名前缀
        String prefix = "";
        //每次迭代的长度限制,最大1000,推荐值 1000
        int limit = 10;
        //指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
        String delimiter = "";

        //列举空间文件列表
        BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);
        while (fileListIterator.hasNext()) {
            //处理获取的file list结果
            FileInfo[] items = fileListIterator.next();
            for (FileInfo item : items) {
                download(item.key);//下载
            }
        }
    }

    /**
     * 下载
     */
    public static void download(String fileName) {
        String domainOfBucket = "cdn.jiagou1216.com";
        String encodedFileName = null;
        try {
            encodedFileName = URLEncoder.encode(fileName, "utf-8").replace("+", "%20");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String publicUrl = String.format("%s/%s", domainOfBucket, encodedFileName);

        Auth auth = Auth.create(accessKey, secretKey);
        long expireInSeconds = 3600;//1小时,可以自定义链接过期时间
        String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
        System.out.println(finalUrl);
        HttpUtil.downloadFile(finalUrl, FileUtil.file("d://七牛云"));
    }
}

 

posted on 2020-03-13 13:53  开发者导航  阅读(2080)  评论(1编辑  收藏  举报