上传头像
添加讲师实现讲师头像上传—阿里云Oss
准备工作
阿里云oss存储服务
1.申请阿里云账号,实名认证,开通“对象存储OSS”服务,进入管理控制台
阿里云网站
2.创建Bucket
选择:标准存储/低频,公共读,
3.文件管理-上传文件
java代码操作阿里云oss,上传文件到阿里云oss操作
(1)准备工作:创建操作阿里云oss许可证(阿里云颁发id和密钥)----创建accesskey
(2)支持与服务-文档与工具-文档中心
基本环境搭建
1.在service模块下创建子模块service_oss
2.在service_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>
3.配置application.yml
服务端口注意区别
#服务端口号
server:
port: 8003
#服务名
spring:
application:
name: service-oss
#环境配置
profiles:
active: dev
#阿里云oss
#不同的服务器,地址不同
aliyun:
oss:
file:
endpoint: your endpoint
keyid: your accessKeyId
keysecret: your assessKeySecret
bucketname: gulixueyuan-data #bucket可以在控制台创建,也可以使用java代码创建
4.创建启动类,报错
启动的时候,找数据库配置,但是模块中不需要操作数据库,只需要上传功能,没有配置数据库
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
解决方式:
1.添加数据库配置
2.在启动类添加属性,默认不去加载数据库配置(推荐)
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
接口完善
1.创建常量类,读取配置文件中的内容
创建常量读取工具类:ConstantPropertiesUtil.java
使用@Value读取application.yml里的配置内容
实现spring中的 InitializingBean接口,实现接口中的 afterPropertiesSet 方法,来初始化配置信息,这个方法会在所有的属性被初始化后调用。
package com.atguigu.oss.utils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//当项目已启动,spring中有一个接口,spring加载之后,执行接口中的一个方法
@Component
public class ConstantPropertiesUtils implements InitializingBean {
//读取配置文件内容
@Value("${aliyun.oss.file.endpoint}") //属性注入,spring中的value
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 KEY_ID;
public static String KEY_SECRET;
public static String BUCKET_NAME;
@Override
public void afterPropertiesSet() throws Exception {
END_POINT = endPoint;
KEY_ID = keyId;
KEY_SECRET = keySecret;
BUCKET_NAME = bucketName;
}
}
2.创建Controller,创建service
package com.atguigu.oss.controller;
import com.atguigu.commonutils.R;
import com.atguigu.oss.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
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
@CrossOrigin
@RequestMapping("/eduoss/fileoss")
public class OssController {
@Autowired
private OssService ossService;
//上传头像的方法
@PostMapping
public R uploadOssFile(MultipartFile file){
//获取上传文件 MultipartFile
//返回上传到oss的路径
String url = ossService.uploadFileAvatar(file);
return R.ok().data("url",url);
}
}
3.在service实现上传文件到Oss的过程
package com.atguigu.oss.service.impl