控制台



后台代码
- 获取访问阿里云API的密钥
- 创建 service_oss 模块
- 引入阿里云oss相关依赖
<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>
- 配置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
- 配置文件工具类
/*** 配置文件工具类:用于读取配置文件中的OSS配置*/
@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_POINT;public static String ACCESS_KEY_ID;public static String ACCESS_KEY_SECRET;public static String BUCKET_NAME;@Overridepublic void afterPropertiesSet() throws Exception {END_POINT = endpoint;ACCESS_KEY_ID = keyId;ACCESS_KEY_SECRET = keySecret;BUCKET_NAME = bucketName;}
}
- 编写启动类
package com.guli.oss;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableSwagger2
public class OssApplication {public static void main(String[] args) {SpringApplication.run(OssApplication.class, args);}
}
- 编写controller
@RestController
@RequestMapping("/eduoss/fileoss")
@CrossOrigin
public class OssController {@Autowiredprivate OssService ossService;@PostMapping("/upload")public R uploadOssFile(MultipartFile file) {// 上传文件,返回文件路径String uploadUrl = ossService.uploadFileAvatar(file);return R.ok().message("文件上传成功").data("url", uploadUrl);}
}
- 编写service类
@Service
public class OssServiceImpl implements OssService {/*** 上传文件流到阿里云 OSS* @param file* @return*/@Overridepublic String uploadFileAvatar(MultipartFile file) {//获取阿里云存储相关常量String endPoint = ConstantPropertiesUtils.END_POINT;String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;String bucketName = ConstantPropertiesUtils.BUCKET_NAME;String uploadUrl = null;try{//判断oss实例是否存在:如果不存在则创建,如果存在则获取OSS ossClient = new OSSClientBuilder().build(endPoint,accessKeyId,accessKeySecret);if (!ossClient.doesBucketExist(bucketName)) {//创建bucketossClient.createBucket(bucketName);//设置oss实例的访问权限:公共读ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);}//获取上传文件流InputStream inputStream = file.getInputStream();/*防止多次上传相同名称文件,造成最后一次上传把之前文件覆盖的现象- 随机文件名- 文件夹分类管理(根据创建日期)构建日期路径:avatar/2019/02/26/文件名 OSS中会创建 avatar,2019,02,06 的层级文件夹*/// 创建时间String datePath = new DateTime().toString("yyyy/MM/dd"); // 使用 joda-time 工具类,将当前时间格式化//文件名:uuid.扩展名String original = file.getOriginalFilename();assert original != null;String fileType = original.substring(original.lastIndexOf("."));// 文件名String fileName = UUID.randomUUID().toString().replaceAll("-",""); // 随机唯一的字符串做文件名String newName = fileName + fileType;// 文件路径String fileUrl = datePath + "/" + newName;//文件上传至阿里云ossClient.putObject(bucketName, fileUrl, inputStream);// 关闭OSSClient。ossClient.shutdown();//获取url地址uploadUrl = "https://" + bucketName + "." + endPoint + "/" + fileUrl;}catch (IOException e){e.printStackTrace();}return uploadUrl;}
}

















