1.创建一个service_ossspringboot项目

2.导入相关依赖
...其他的依赖大家自行导入
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
3.写yml配置文件
server:
port: 8205
spring:
application:
name: service-oss
profiles:
active: dev
servlet:
multipart:
max-file-size: 1024MB
max-request-size: 1024MB
aliyun:
oss:
bucketname: startqbb-oss
endpoint: oss-cn-hangzhou.aliyuncs.com
keyid: your accessKeyId
keysecret: your accessKeySecret
4.主启动类
package com.qbb.yygh.oss;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class ServiceOssApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceOssApplication.class, args);
}
}
5.创建一个properties配置类,绑定oss配置信息
package com.qbb.yygh.oss.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class OssProperties {
private String endpoint;
private String keyid;
private String keysecret;
private String bucketname;
}
6.controller层,文件上传三要素
package com.qbb.yygh.oss.controller;
import com.qbb.yygh.oss.service.FileService;
import com.qbb.yygh.result.R;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@Api(tags = "阿里云OSS文件存储服务")
@RestController
@RequestMapping("/api/oss")
public class FileController {
@Autowired
private FileService fileService;
@ApiOperation("阿里云OSS文件上传")
@PostMapping("/upload")
public R upload(@ApiParam("上传的文件") @RequestParam("file") MultipartFile file) {
String url = fileService.upload(file);
return R.ok().data("url", url);
}
}
7.service上传核心代码,推荐参考官方文档
package com.qbb.yygh.oss.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.qbb.yygh.oss.properties.OssProperties;
import com.qbb.yygh.oss.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.UUID;
@Service
@Slf4j
public class FileServiceImpl implements FileService {
@Autowired
private OssProperties ossProperties;
@Override
public String upload(MultipartFile file) {
String endpoint = ossProperties.getEndpoint();
String accessKeyId = ossProperties.getKeyid();
String accessKeySecret = ossProperties.getKeysecret();
String bucketName = ossProperties.getBucketname();
String randomStr = UUID.randomUUID().toString().replaceAll("-", "") + file.getOriginalFilename();
String dateStr = new DateTime().toString("yyyy/MM/dd");
String objectName = dateStr + "/" + randomStr;
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
InputStream inputStream = file.getInputStream();
ossClient.putObject(bucketName, objectName, inputStream);
StringBuilder sb = new StringBuilder();
sb.append("https://")
.append(bucketName)
.append(".")
.append(endpoint)
.append("/")
.append(objectName);
return sb.toString();
} catch (Exception oe) {
log.info("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
return null;
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
8.测试一下


整合完毕
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探