一、pom依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>service</artifactId> <groupId>com.caiweiwei</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>service_oss</artifactId> <dependencies> <!--阿里云oss依赖--> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> </dependency> <!--日期工具依赖--> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> </dependency> </dependencies> </project>
二、application文件配置
#服务器端口号 server.port=8002 #服务名 spring.application.name=service-oss #环境设置 spring.profiles.active=dev #配置阿里云 oss aliyun.oss.file.endpoint="你的节点" aliyun.oss.file.keyid="开启oss服务生成秘钥的keyid" aliyun.oss.file.keysecret="生成秘钥的密码" aliyun.oss.file.bucketname="自己创建bucket的名称"
三、编写工具类进行对application配置项的获取
package com.caiweiwei.oss.utils; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component//交给spring管理,spring加载之后,执行接口的一个方法 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_POINT; public static String KTY_ID; public static String KEY_SECRET; public static String BUCKET_NAME; @Override public void afterPropertiesSet() throws Exception { END_POINT=endpoint; KTY_ID=keyId; KEY_SECRET=keySecret; BUCKET_NAME=bucketName; } }
四、编写控制层代码
package com.caiweiwei.oss.controller; import com.caiweiwei.commonutils.R; import com.caiweiwei.oss.service.OssService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController @RequestMapping("/eduoss/fileoss") @CrossOrigin public class OssController { @Autowired private OssService ossService; /** * 功能描述:头像上传 * @Author: caiweiwei * @Date: 2020/9/15 9:05 */ @PostMapping("/upload") public R uploadOssFile(MultipartFile file) { //获取文件上传路径 //返回上传到oss的路径 String url = ossService.uploadFile(file); return R.ok().data("url", url); } }
五、编写service实现类
package com.caiweiwei.oss.service.impl; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.caiweiwei.oss.service.OssService; import com.caiweiwei.oss.utils.ConstantPropertiesUtils; import org.joda.time.DateTime; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; import java.util.UUID; @Service public class OssServiceImpl implements OssService { /** * 功能描述:头像上传使用oss功能实现 * * @Author: caiweiwei * @Date: 2020/9/15 9:06 */ @Override public String uploadFile(MultipartFile file) { String endpoint = ConstantPropertiesUtils.END_POINT; String accsessKeyId = ConstantPropertiesUtils.KTY_ID; String accsessKeySecret = ConstantPropertiesUtils.KEY_SECRET; String bucketName = ConstantPropertiesUtils.BUCKET_NAME; try { //创建OSSClient实例 OSS ossClient = new OSSClientBuilder().build(endpoint, accsessKeyId, accsessKeySecret); //获取上传文件的输入流 InputStream inputStream = file.getInputStream(); //获取文件的名称 String filename = file.getOriginalFilename(); //使用uuid进行重命名 filename=UUID.randomUUID().toString().replaceAll("-","")+filename; //将文件按照日期进行分类 String datePath=new DateTime().toString("yyyy/MM/dd"); filename=datePath+"/"+filename; //调用oss方法实现文件上传 //第一个参数 bucket名称 //第二个参数 上传到oss文件路径和文件名称 //第三个参数 上传文件输入流 ossClient.putObject(bucketName, filename, inputStream); //关闭ossclient ossClient.shutdown(); //将上传的路径返回 String url = "https://" + bucketName + "." + endpoint + "/" + filename; return url; } catch (Exception e) { e.printStackTrace(); return null; } } }