springboot整合oss实现上传,查看,下载
时间: 2023-06-12 21:08:24 浏览: 120
Spring Boot 是一种快速开发应用程序的框架,而OSS(Object Storage Service)是阿里云提供的对象存储服务。将它们结合起来,可以实现文件的上传、下载、查看等功能。
下面是实现上传、查看、下载的具体步骤:
1. 引入依赖
在 `pom.xml` 文件中添加以下依赖:
```xml
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
```
2. 配置 OSS 客户端
在配置文件中添加 OSS 的配置:
```properties
aliyun.oss.endpoint=oss-cn-hangzhou.aliyuncs.com
aliyun.oss.accessKeyId=yourAccessKeyId
aliyun.oss.accessKeySecret=yourAccessKeySecret
aliyun.oss.bucketName=yourBucketName
```
然后创建 OSS 客户端:
```java
@Configuration
public class OSSConfig {
@Autowired
private OSSProperties ossProperties;
@Bean
public OSS ossClient() {
return new OSSClientBuilder().build(ossProperties.getEndpoint(),
ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());
}
}
```
3. 实现上传功能
在控制器中实现上传功能:
```java
@RestController
public class OSSController {
@Autowired
private OSS ossClient;
@Autowired
private OSSProperties ossProperties;
@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) throws IOException {
// 上传文件到 OSS
String filename = file.getOriginalFilename();
ossClient.putObject(ossProperties.getBucketName(), filename, file.getInputStream());
// 返回文件 URL
return ossClient.generatePresignedUrl(ossProperties.getBucketName(), filename,
new Date(System.currentTimeMillis() + 3600 * 1000)).toString();
}
}
```
4. 实现查看功能
在控制器中实现查看功能:
```java
@RestController
public class OSSController {
@Autowired
private OSS ossClient;
@Autowired
private OSSProperties ossProperties;
@GetMapping("/view/{filename:.+}")
public ResponseEntity<Resource> view(@PathVariable String filename) throws IOException {
// 从 OSS 中获取文件流
OSSObject ossObject = ossClient.getObject(ossProperties.getBucketName(), filename);
InputStream inputStream = ossObject.getObjectContent();
// 将文件流转换为 Resource 对象
ByteArrayResource resource = new ByteArrayResource(IOUtils.toByteArray(inputStream));
// 返回 Resource 对象
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")
.contentLength(resource.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);
}
}
```
5. 实现下载功能
在控制器中实现下载功能:
```java
@RestController
public class OSSController {
@Autowired
private OSS ossClient;
@Autowired
private OSSProperties ossProperties;
@GetMapping("/download/{filename:.+}")
public ResponseEntity<Resource> download(@PathVariable String filename) throws IOException {
// 从 OSS 中获取文件流
OSSObject ossObject = ossClient.getObject(ossProperties.getBucketName(), filename);
InputStream inputStream = ossObject.getObjectContent();
// 将文件流转换为 Resource 对象
ByteArrayResource resource = new ByteArrayResource(IOUtils.toByteArray(inputStream));
// 返回 Resource 对象
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")
.contentLength(resource.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);
}
}
```
这样就可以通过访问 `/upload` 接口上传文件,访问 `/view/{filename}` 接口查看文件,访问 `/download/{filename}` 接口下载文件了。
阅读全文