相信大家在做web开发过程中肯定会遇到头像上传功能,这里使用OSS进行头像上传存储
先登录:
这里最好选择支付宝扫码登录,相信大家都进行了支付宝的实名认证
如果可以的话,可以往账户里存1块钱,这样不会造成存储量过大欠费
然后选择产品中的存储,OSS
进来后选择开通,因为我实现已经开通好了,所以我这里是管理控制台
下面进入后台接口编写
首先,创建一个对外访问接口
import com.atguigu.commonutils.Result;
import com.atguigu.oss.service.impl.FileServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@Api(description = "阿里云文件管理")
@RestController
@RequestMapping("/oss/file")
@CrossOrigin
publicclass FileController {
@Autowired
private FileServiceImpl fileService;
@ApiOperation(value = "文件上传")
@PostMapping("/upload")
public Result upload(@ApiParam(name = "file",value = "文件",required = true) @RequestParam("file") MultipartFile multipartFile){
String url = (multipartFile);
return Result.succ().message("文件上传成功").data("url",url);
}
}
编写service层接口
import org.springframework.web.multipart.MultipartFile;
publicinterface FileService {
/**
* 文件上传至阿里云
* @param file
* @return*/
String upload(MultipartFile file);
}
实现接口
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.atguigu.oss.service.FileService;
import com.atguigu.oss.util.ConstantPropertiesUtil;
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
publicclass FileServiceImpl implements FileService {
/**
* 文件上传至阿里云
*
* @param file
* @return*/
@Override
public String upload(MultipartFile file) {
String endPoint = ConstantPropertiesUtil.END_POINT;
String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtil.BUCKET_NAME;
String uploadUrl = null;
try {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
if (!(bucketName)){
//创建bucket ossClient.createBucket(bucketName);
//设置oss实例的访问权限:公共读 (bucketName, );
}
// 获取上传文件流。
InputStream inputStream = ();
String filename = ();
String uuid = ().toString().replaceAll("-","");
filename = uuid+filename;
String datePath = new DateTime().toString("yyyy/MM/dd");
filename = datePath+"/"+filename;
//文件上传至阿里云 (bucketName, filename, inputStream);
//关闭OSSClient ossClient.shutdown();
uploadUrl = "https://"+bucketName+"."+endPoint+"/"+filename;
}catch (Exception e){
();
}
return uploadUrl;
}
}
这里因为需要用到几个固定值
所以我写在了配置文件中
#服务端口
server.port=8002
#服务名
spring.application.name=service-oss
#环境设置:dev、test、prod
spring.profiles.active=dev
#阿里云 OSS
#不同的服务器,地址不同
=your
=your
=your
#bucket可以在控制台创建,也可以使用java代码创建
=your
下面写一个工具类来取出值
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
publicclass ConstantPropertiesUtil implements InitializingBean {
@Value("${}")
private String endpoint;
@Value("${}")
private String keyId;
@Value("${}")
private String keySecret;
@Value("${}")
private String bucketName;
publicstatic String END_POINT;
publicstatic String ACCESS_KEY_ID;
publicstatic String ACCESS_KEY_SECRET;
publicstatic String BUCKET_NAME;
@Override
publicvoid afterPropertiesSet() throws Exception {
END_POINT = endpoint;
ACCESS_KEY_ID = keyId;
ACCESS_KEY_SECRET = keySecret;
BUCKET_NAME = bucketName;
}
}
然后使用swagger进行测试
查看数据库
存储成功
进入OSS文件目录中
文件存储成功
这里前端我使用的是vue+element-ui
觉得有用的小伙伴记得点个收藏哦~