以下是 spring boot 对接七牛云存储的详细步骤和流程,结合了多个开发实践和最佳方案:
一、前期准备
注册七牛云账号
- 访问七牛云官网,完成注册并登录。
- 进入「密钥管理」获取
access key
和secret key
(用于鉴权)。
创建存储空间(bucket)
- 在控制台选择「对象存储」,创建新的存储空间,命名后记录
bucket 名称
。 - 设置存储空间属性(如存储类型、地域等),获取存储空间的访问域名(如
http://xxx.bkt.clouddn.com
)。
二、spring boot 项目配置
1. 添加依赖
在 pom.xml
中引入七牛云 java sdk:
<dependency> <groupid>com.qiniu</groupid> <artifactid>qiniu-java-sdk</artifactid> <version>[7.2.0, 7.7.99]</version> </dependency>
注意:版本号需根据最新网页调整。
2. 配置参数
在 application.yml
中添加七牛云配置:
qiniu: accesskey: your_access_key secretkey: your_secret_key bucket: your_bucket_name domain: http://xxx.bkt.clouddn.com # 存储空间访问域名 region: huadong # 存储空间所在地域(如华东:huadong)
3. 配置类
创建 qiniuproperties
类绑定配置:
@data @configurationproperties(prefix = "qiniu") public class qiniuproperties { private string accesskey; private string secretkey; private string bucket; private string domain; private string region; }
三、实现文件上传功能
1. 工具类开发
创建 qiniuutils
工具类,封装上传逻辑:
@component public class qiniuutils { @autowired private qiniuproperties qiniuproperties; // 上传文件到七牛云 public string uploadfile(multipartfile file) { try { configuration cfg = new configuration(region.region2()); // 根据实际地域选择 uploadmanager uploadmanager = new uploadmanager(cfg); auth auth = auth.create(qiniuproperties.getaccesskey(), qiniuproperties.getsecretkey()); string uptoken = auth.uploadtoken(qiniuproperties.getbucket()); // 生成唯一文件名(避免重复) string filename = uuid.randomuuid().tostring() + "_" + file.getoriginalfilename(); // 上传并解析结果 response response = uploadmanager.put(file.getbytes(), filename, uptoken); defaultputret putret = json.parseobject(response.bodystring(), defaultputret.class); // 返回完整访问路径 return qiniuproperties.getdomain() + "/" + putret.key; } catch (exception e) { throw new runtimeexception("上传失败:" + e.getmessage()); } } }
关键点:
- 使用 region 指定存储区域(如 region.huadong() 对应华东)。
- 文件名通过 uuid 生成唯一标识,保留原始后缀。
2. controller 层
创建接口接收前端上传请求:
@restcontroller @requestmapping("/file") public class filecontroller { @autowired private qiniuutils qiniuutils; @postmapping("/upload") public r uploadfile(@requestparam("file") multipartfile file) { string url = qiniuutils.uploadfile(file); return r.success(url); } }
四、扩展功能
1. 文件下载
通过七牛云域名直接访问文件,或生成带签名的临时下载链接:
public string generatedownloadurl(string key) { auth auth = auth.create(accesskey, secretkey); return auth.privatedownloadurl(domain + "/" + key, 3600); // 1小时有效 }
2. 文件删除
使用 bucketmanager
删除指定文件:
public void deletefile(string key) { auth auth = auth.create(accesskey, secretkey); bucketmanager bucketmanager = new bucketmanager(auth, config); bucketmanager.delete(bucketname, key); }
3. 文件列表查询
遍历存储空间中的文件(分页处理):
public list<string> listfiles(string prefix, string marker) { auth auth = auth.create(accesskey, secretkey); bucketmanager bucketmanager = new bucketmanager(auth, config); bucketmanager.filelistiterator filelistiterator = bucketmanager.createfilelistiterator(bucketname, prefix, 1000, marker); list<string> filelist = new arraylist<>(); while (filelistiterator.hasnext()) { fileinfo[] items = filelistiterator.next(); for (fileinfo item : items) { filelist.add(item.key); } } return filelist; }
五、前端集成示例
1. html 表单
<form action="/file/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <button type="submit">上传</button> </form>
2. ajax 上传(jquery)
$.ajax({ url: "/file/upload", type: "post", data: new formdata($("#uploadform")[0]), processdata: false, contenttype: false, success: function(res) { console.log("上传成功:" + res.data); } });
六、注意事项
文件大小限制
- 在
application.yml
中配置:
spring: servlet: multipart: max-file-size: 10mb max-request-size: 50mb
异常处理
- 添加全局异常处理器,捕获七牛云 sdk 抛出的
qiniuexception
。
安全性
- 敏感操作(如删除)需添加权限校验。
- 临时下载链接建议设置短有效期。
通过以上步骤,即可实现 spring boot 与七牛云存储的完整对接,支持文件上传、下载、删除及列表查询等核心功能。实际开发中可根据需求扩展图片处理(如缩略图)、水印添加等高级功能。
以上就是spring boot对接七牛云存储的详细步骤和流程的详细内容,更多关于spring boot对接七牛云存储的资料请关注代码网其它相关文章!
发表评论