目录
一.OSS服务器开通并创建账户
二.Springboot整合OSS
1.创建springboot项目
2.整合OSS
三.postman测试
一.OSS服务器开通并创建账户
参考阿里云OSS的使用(全程请登陆)_zhz小白的博客-CSDN博客https://blog.csdn.net/zhouhengzhe/article/details/112077301
二.Springboot整合OSS
1.创建springboot项目
1.1勾选web依赖
注意:如果jdk版本为1.8,则springboot版本应该为3.0以下
1.2 引入相关依赖
<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.10.2</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version><scope>compile</scope></dependency>
2.整合OSS
2.1启动文件配置
server:port: 8080
spring:application:name: oss-server
aliyun: oss:end-point: oss-cn-chengdu.aliyuncs.comaccess-key-id: LTAI5tCNDUQeX9PtAv9nZmMfaccess-key-secret: KGVGWz2H9yyrogOilJ4K1dgr8EX7Xhbucket-name: oss-test-cheng
2.2 OSS配置类OSSConfig
@Configuration
@Data
@ConfigurationProperties(prefix = "aliyun.oss")
public class OSSConfig {private String endPoint;private String accessKeyId;private String accessKeySecret;private String bucketName;
}
2.3 服务层代码
public interface FileService {String upload(MultipartFile file) throws IOException;void download() throws IOException;
}
@Service
@Slf4j
public class FileServiceImpl implements FileService {@Autowiredprivate OSSConfig ossConfig;@Overridepublic String upload(MultipartFile file) throws IOException {String endPoint = ossConfig.getEndPoint();String accessKeyId = ossConfig.getAccessKeyId();String accessKeySecret = ossConfig.getAccessKeySecret();String bucketName = ossConfig.getBucketName();//创建oss对象OSS oss = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);String originalFilename = file.getOriginalFilename();//设置日期格式LocalDateTime now = LocalDateTime.now();DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");String date = dateTimeFormatter.format(now);//拼装oss文件的存储路径String fileName= UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);String extension=originalFilename.substring(originalFilename.lastIndexOf("."));//在oss上bucket下的文件路径String uploadFileName = "user/" + date + "/" + fileName + extension;PutObjectResult putObjectResult = oss.putObject(bucketName, uploadFileName, file.getInputStream());if (putObjectResult!=null){log.info("文件上传成功");return "https://"+bucketName+"."+endPoint+"/"+uploadFileName;}else {log.info("文件上传失败");}oss.shutdown();return null;}@Overridepublic void download() throws IOException {String endPoint = ossConfig.getEndPoint();String accessKeyId = ossConfig.getAccessKeyId();String accessKeySecret = ossConfig.getAccessKeySecret();String bucketName = ossConfig.getBucketName();OSS oss = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);//获取OSS端的文件OSSObject result = oss.getObject(bucketName, "235920pM89Q.jpg");//创建输入流BufferedInputStream inputStream = new BufferedInputStream(result.getObjectContent(), 512);String[] split = result.getKey().split("/");String fileName = split[split.length - 1];//创建输出流FileOutputStream outputStream= new FileOutputStream("C:\\Users\\13404\\Pictures\\Camera Roll\\write.jpg");//处理oss文件流传输int number;/**/byte[] buffer = new byte[512];while ((number = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, number);}outputStream.flush();outputStream.close();inputStream.close();log.info("下载完毕");}
}
2.4 控制层代码
@RestController
@Slf4j
@RequestMapping("/file")
public class FileController {@Autowiredprivate FileService fileService;@PostMapping("/upload")public Map<String,Object> upload(@RequestParam("file") MultipartFile file) throws IOException {String upload = fileService.upload(file);Map<String, Object> result = new HashMap<>();if(upload== null || "".equals(upload)){result.put("code",10000);result.put("msg","图片上传失败");result.put("data",null);}else{result.put("code",200);result.put("msg","图片上传成功");result.put("data",upload);}return result;}@GetMapping("/download")public void download() throws IOException {fileService.download();}}
三.postman测试
upload测试参数设置如下:
结果:
download测试:这里我上传和下载的不是一张图片
结果: