原文摘自: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://七牛云"));
}
}

springboot获取七牛云空间文件列表及下载功能的更多相关文章

  1. PHP获取Linux当前目录下文件并实现下载功能

    使用nginx转发过去给php server{ listen 9099; server_name 18.5.6.2; location / { proxy_http_version 1.1; root ...

  2. 利用cropper插件裁剪本地图片,然后将裁剪过后的base64图片上传至七牛云空间

    现在做的项目需要做一些图片处理,由于时间赶急,之前我便没有处理图片,直接将图片放在input[type=file]里面,以文件的形式提交给后台,这样做简直就是最低级的做法,之后各种问题便出来了,人物头 ...

  3. C#获取七牛云token/删除七牛云图片接口

    // 获取七牛token public ApiResponse GetQiniuToken(QiniuToken req) { try { Mac mac = new Mac(req.AccessKe ...

  4. C/C++ 获取目录下的文件列表信息

    在C/C++编程时,需要获取目录下面的文件列表信息. 1.数据结构 struct dirent {     long d_ino;                 /* inode number 索引 ...

  5. JavaWeb实现文件上传下载功能实例解析

    转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...

  6. JavaWeb实现文件上传下载功能实例解析 (好用)

    转: JavaWeb实现文件上传下载功能实例解析 转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web ...

  7. Poco之ftp获取文件列表以及下载文件

    #include <iostream>#include <string>#include <vector>#include <algorithm>#in ...

  8. C# 文件上传下载功能实现 文件管理引擎开发

    Prepare 本文将使用一个NuGet公开的组件技术来实现一个服务器端的文件管理引擎,提供了一些简单的API,来方便的实现文件引擎来对您自己的软件系统的文件进行管理. 在Visual Studio ...

  9. php实现文件上传下载功能小结

    文件的上传与下载是项目中必不可少的模块,也是php最基础的模块之一,大多数php框架中都封装了关于上传和下载的功能,不过对于原生的上传下载还是需要了解一下的.基本思路是通过form表单post方式实现 ...

  10. 文件一键上传、汉字转拼音、excel文件上传下载功能模块的实现

    ----------------------------------------------------------------------------------------------[版权申明: ...

随机推荐

  1. 使用 Docker 部署 Next Terminal 轻量级堡垒机

    1)Next Terminal 介绍 官网:https://next-terminal.typesafe.cn/ GitHub:https://github.com/dushixiang/next-t ...

  2. XAF Blazor FilterPanel

    前言 XAF列表视图(ListView)中的过滤(Filter),可以在ListView模型的Filters节点中添加,添加的过滤项(FilterItem)会在列表视图的工具栏中以下拉列表的形式显示, ...

  3. jQuery 框架

    jQuery 框架 目录 jQuery 框架 一. 概述 二. jQuery 安装引用 2.1 安装 2.2 本地导入使用 2.3 jQuery CDN引入 三. jQuery基本语法 四. 查找标签 ...

  4. 关闭 nginx | taskkill /f /t /im nginx.exe

    stop.bat taskkill /f /t /im nginx.exe pause

  5. Vue 动态插入组件 用js函数的方式

    Vue 动态插入组件 用js函数的方式 第一步 import vue组件 第二步 Vue把组件扩展进去 第三步 创建实例 第四步 将组件的el挂载到document.body上 第五步 设置组件内部d ...

  6. C++学习笔记之指针引用

    目录 指针 指针定义 左值与右值 指针数组与数组指针 const与指针 指针的指针 NULL指针 内存泄漏(Memory Leak)问题 智能指针 引用 指针 指针定义 指针定义的基本形式:指针本身就 ...

  7. SQL注入详细讲解概括—盲注

    SQL注入详细讲解概括-盲注 1.盲注简单理解 2.盲注必学函数 3.布尔盲注 4.时间盲注 一.盲注简单理解 What is 盲注? It is 在服务器没有错误回显的时候完成的注入攻击 数据库把报 ...

  8. Java加密技术(四)——非对称加密算法RSA

    Java非对称加密算法rsa     接下来我们介绍典型的非对称加密算法--RSA RSA     这种算法1978年就出现了,它是第一个既能用于数据加密也能用于数字签名的算法.它易于理解和操作,也很 ...

  9. FAT32 文件系统详解

    PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明   本文作为本人csdn blog的主站的备份.(Bl ...

  10. Welcome to YARP - 2.3 配置功能 - 配置过滤器(Configuration Filters)

    目录 Welcome to YARP - 1.认识YARP并搭建反向代理服务 Welcome to YARP - 2.配置功能 2.1 - 配置文件(Configuration Files) 2.2 ...