Springboot集成阿里云OSS上传文件系统教程
程序员文章站
2022-03-17 19:32:42
第一步:开通阿里云oss服务,创建bucket,获取id和密钥第二步:根据官方文档编写上传代码1.新建maven项目添加依赖:
第一步:开通阿里云oss服务,创建bucket,获取id和密钥
第二步:根据官方文档编写上传代码
1.新建maven项目
添加依赖:
<!-- 阿里云oss依赖 --> <dependency> <groupid>com.aliyun.oss</groupid> <artifactid>aliyun-sdk-oss</artifactid> </dependency> <!-- 日期工具栏依赖 --> <dependency> <groupid>joda-time</groupid> <artifactid>joda-time</artifactid> </dependency>
2.配置application.properties
#服务端口 server.port=8002 #服务名 spring.application.name=service-oss #环境设置:dev、test、prod spring.profiles.active=dev #阿里云 oss #不同的服务器,地址不同 需要根据自己的信息填写 aliyun.oss.file.endpoint=your endpoint aliyun.oss.file.keyid=your accesskeyid aliyun.oss.file.keysecret=your accesskeysecret #bucket可以在控制台创建,也可以使用java代码创建 aliyun.oss.file.bucketname=guli-file
3、创建启动类
创建ossapplication.java
package com.sun.oss; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.context.annotation.componentscan; @springbootapplication @componentscan({"com.sun"}) public class ossapplication { public static void main(string[] args) { springapplication.run(ossapplication.class, args); } }
启动报错:
多模块应用时,该模块没有使用数据库,配置文件中没有数据库配置信息,则会报错
第一种方法:
在配置类中添加数据库配置信息:
# mysql数据库连接 spring.datasource.driver-class-name=com.mysql.cj.jdbc.driver spring.datasource.url=jdbc:mysql://localhost:3306/musicdb?servertimezone=gmt%2b8 spring.datasource.username=root spring.datasource.password=123456
第二种方法:添加注解,在@springbootapplication注解上加上exclude,解除自动加载datasourceautoconfiguration
@springbootapplication(exclude = datasourceautoconfiguration.class)
4.编写一个工具类:
用于读取配置文件密钥信息
constantpropertiesutils.java
//当项目已启动,spring接口,spring加载之后,执行接口一个方法 @component public class constantpropertiesutils implements initializingbean { //读取配置文件内容 @value("${aliyun.oss.file.endpoint}") private string endpoint; @value("${aliyun.oss.file.keyid}") private string keyid; @value("${aliyun.oss.file.keysecret}") private string keysecret; @value("${aliyun.oss.file.bucketname}") private string bucketname; //定义公开静态常量 public static string end_poind; public static string access_key_id; public static string access_key_secret; public static string bucket_name; @override public void afterpropertiesset() throws exception { end_poind = endpoint; access_key_id = keyid; access_key_secret = keysecret; bucket_name = bucketname; } }
5.编写controller类
@api(description = "头像上传到oss") @restcontroller @requestmapping("/eduoss/fileoss") @crossorigin//解决跨域问题 public class osscontroller { @autowired private ossservice ossservice; //上传头像的方法 @apioperation("上传头像") @postmapping public r uploadossfile(multipartfile file) { //获取上传文件 multipartfile //返回上传到oss的路径 string url = ossservice.uploadfileavatar(file); return r.ok().data("url",url); } }
6.编写service类
@service public class ossserviceimpl implements ossservice { //上传头像 @override public string uploadfileavatar(multipartfile file) { //工具类获取值 string endpoind = constantpropertiesutils.end_poind; string accesskeyid = constantpropertiesutils.access_key_id; string accesskeysecret = constantpropertiesutils.access_key_secret; string bucketname = constantpropertiesutils.bucket_name; try { //创建oss实例 oss ossclient = new ossclientbuilder().build(endpoind, accesskeyid, accesskeysecret); //获取上传文件输入流 inputstream inputstream = file.getinputstream(); //获取文件名称 string filename = file.getoriginalfilename(); //1.防止上传文件名重复,使用uuid string uuid = uuid.randomuuid().tostring().replaceall("-", ""); //拼接filename filename=uuid+filename; //2.把文件按照日期分类 //获取当前日期,需要引入org.joda.time依赖 string datepath = new datetime().tostring("yyyy/mm/dd"); //拼接 filename=datepath+"/"+filename; //调用oss方法实现上传 //第一个参数 bucket名称 //第二个参数 上传到oss的文件路径和文件名 //第三个参数 上传文件输入流 ossclient.putobject(bucketname,filename,inputstream); //关闭ossclient ossclient.shutdown(); //上传之后把文件路劲返回 //需要把上传到oss路径手动拼接出来 //https://edu-975.oss-cn-beijing.aliyuncs.com/07aef13af7ea82703f3eb320c3f577ec.jpg string url="https://"+bucketname+"."+endpoind+"/"+filename; return url; }catch (exception e){ e.printstacktrace(); return null; } } }
解决上传文件大小:
# 单个文件大小 spring.servlet.multipart.max-request-size=10mb # 单次最大文件 spring.servlet.multipart.max-file-size=3mb
swagger测试运行:
成功!!
到此这篇关于springboot集成阿里云oss上传文件系统教程的文章就介绍到这了,更多相关springboot集成阿里云oss文件上传内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 柯尼卡美能达打印机打印出现乱乱码怎么办?
下一篇: 你当老子没当过兵啊