maven导入依赖
org.springframework.boot spring-boot-starter-thymeleaforg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-devtoolsruntime true mysql mysql-connector-javaruntime org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest com.alibaba fastjson1.2.15 commons-lang commons-lang2.6 com.aliyun.oss aliyun-sdk-oss3.10.2
定义阿里云上传结果实体
package com.example.demo.entity;
import lombok.Data;
@Data
public class AliyunOssResult {
private int code;
private String url;
private String msg;
}
yml设置阿里云oss参数
aliyunOss: endpoint: "http://oss-cn-shanghai.aliyuncs.com" accessKeyId: "xxxxxxx" accessKeySecret: "xxxxxxx" bucketName: "xxxxxx" urlPrefix: "http://bucketName.oss-cn-shanghai.aliyuncs.com/"
yml设置上传文件大小限制
spring: servlet: multipart: max-file-size: 20MB max-request-size: 20MB
工具类封装
package com.example.demo.util;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.example.demo.entity.AliyunOssResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;
@Component
public class AliyunOSSUtil {
@Value("${aliyunOss.endpoint}")
private String endpoint;
@Value("${aliyunOss.accessKeyId}")
private String accessKeyId;
@Value("${aliyunOss.accessKeySecret}")
private String accessKeySecret;
@Value("${aliyunOss.bucketName}")
private String bucketName;
@Value("${aliyunOss.urlPrefix}")
private String urlPrefix;
public AliyunOssResult upload(InputStream inputStream, String objectName) {
AliyunOssResult aliyunOssResult = new AliyunOssResult();
try {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上传内容到指定的存储空间(bucketName)并保存为指定的文件名称(objectName)。
ossClient.putObject(bucketName, objectName, inputStream);
// 关闭OSSClient。
ossClient.shutdown();
aliyunOssResult.setCode(200);
aliyunOssResult.setUrl(urlPrefix+objectName);
aliyunOssResult.setMsg("上传成功");
} catch (Exception e) {
e.printStackTrace();
aliyunOssResult.setCode(400);
aliyunOssResult.setMsg("上传失败");
}
return aliyunOssResult;
}
public void delete(String objectName) {
try {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 删除文件。
ossClient.deleteObject(bucketName, objectName);
// 关闭OSSClient。
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
public void delete(List objectNames) {
try {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 批量删除文件。
DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(objectNames));
List deletedObjects = deleteObjectsResult.getDeletedObjects();
// 关闭OSSClient。
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
public String getUrl(String objectName,long effectiveTime){
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 设置URL过期时间
Date expiration = new Date(new Date().getTime() + effectiveTime);
GeneratePresignedUrlRequest generatePresignedUrlRequest ;
generatePresignedUrlRequest =new GeneratePresignedUrlRequest(bucketName, objectName);
generatePresignedUrlRequest.setExpiration(expiration);
URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
return url.toString();
}
}
controller接收调用
package com.example.demo.controller;
import com.example.demo.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;
@RestController
@RequestMapping("/file")
public class FileController {
@Autowired
private AliyunOSSUtil aliyunOSSUtil;
@RequestMapping(value = "/uploadFile")
public @ResponseBody
Object uploadFile(@RequestParam(value = "file", required = false) MultipartFile file,
String strPath) throws IOException {
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
String objectName = strPath+"/"+ UUID.randomUUID().toString()+"."+suffix;
return aliyunOSSUtil.upload(file.getInputStream(),objectName);
}
}
postman测试
到此这篇关于springBoot接入阿里云oss的实现步骤的文章就介绍到这了,更多相关springBoot接入阿里云oss内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!



