springboot简单整合阿里云oss实现文件上传
1.注册阿里云oss 账号
2.创建bucket
3.导入依赖
<!--aliyunOSS 依赖-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
4.配置application 配置文件
#阿里云 OSS配置
aliyun.oss.file.endpoint=你的服务器地址,不同的服务器,地址不同
aliyun.oss.file.keyid=你的keyid
aliyun.oss.file.keysecret=key的秘钥
#bucket可以在控制台创建,也可以使用java代码创建
aliyun.oss.file.bucketname=你创建的bucket
aliyun.oss.file.filehost=上传的目录
5.创建读取 application 阿里云配置的类
/**
* 常量类,读取配置文件application.properties中的配置
*/
@Component
public class ConstantPropertiesUtil 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.filehost}")
private String fileHost;
@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;
public static String FILE_HOST;
@Override
public void afterPropertiesSet() throws Exception {
END_POINT = endpoint;
ACCESS_KEY_ID = keyId;
ACCESS_KEY_SECRET = keySecret;
BUCKET_NAME = bucketName;
FILE_HOST = fileHost;
}
}
6.编写上传文件接口
-
创建接口
/** * 文件上传至阿里云 * @param file * @return 返回图片路径 */ String Fileupload(MultipartFile file);
-
编写实现类
@Override public String Fileupload(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 fileHost = ConstantPropertiesUtil.FILE_HOST; String uploadUrl = null; try { //判断oss实例是否存在:如果不存在则创建,如果存在则获取 OSSClient ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret); if (!ossClient.doesBucketExist(bucketName)) { //创建bucket ossClient.createBucket(bucketName); //设置oss实例的访问权限:公共读 ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead); } //获取上传文件流 InputStream inputStream = file.getInputStream(); //构建日期路径:avatar/2019/11/19/文件名 String filePath = new DateTime().toString("yyyy/MM/dd"); //文件名:uuid.扩展名 防止文件重名 String original = file.getOriginalFilename(); String fileName = UUID.randomUUID().toString(); String fileType = original.substring(original.lastIndexOf(".")); String newName = fileName + fileType; String fileUrl = fileHost + "/" + filePath + "/" + newName; //文件上传至阿里云 ossClient.putObject(bucketName, fileUrl, inputStream); // 关闭OSSClient。 ossClient.shutdown(); //获取oss图片路径地址 url地址返回出去 用于数据库的保存 uploadUrl = "http://" + bucketName + "." + endPoint + "/" + fileUrl; } catch (IOException e) { throw new KetaiException(ResultCodeEnum.FILE_UPLOAD_ERROR); } return uploadUrl; }
7.测试文件是否能上传
-
编写FileController
@Api(tags = "阿里云文件管理") @RestController @RequestMapping("/admin/edu/file") public class FileController { @Autowired private OSSFileService fileService; /** * 文件上传 * * @param file */ @ApiOperation(value = "文件上传") @PostMapping("upload") public R upload( @ApiParam(name = "file", value = "文件", required = true) @RequestParam("file") MultipartFile file) { String uploadUrl = fileService.Fileupload(file); //返回url对象 用于前端页面图片的回显 return R.ok().message("文件上传成功").data("url", uploadUrl); } }
-
通过swagger 或者postman 测试!!