springboot实现oss文件下载
时间: 2023-08-24 16:15:15 浏览: 61
在Spring Boot中实现OSS文件下载,你可以按照以下步骤进行操作:
1. 首先,你需要引入阿里云OSS的Java SDK依赖。在你的`pom.xml`文件中添加以下依赖:
```xml
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.9.2</version>
</dependency>
```
2. 在Spring Boot的配置文件中,添加OSS相关的配置项,包括`accessKey`、`secretKey`、`endpoint`和`bucketName`等信息。
```properties
# OSS配置
spring.aliyun.oss.access-key=your-access-key
spring.aliyun.oss.secret-key=your-secret-key
spring.aliyun.oss.endpoint=your-endpoint
spring.aliyun.oss.bucket-name=your-bucket-name
```
3. 创建一个用于处理文件下载的Controller。在该Controller中,注入`AliyunOSSClient`对象,并实现一个下载文件的接口。
```java
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.InputStream;
import java.net.URLEncoder;
@RestController
@RequestMapping("/files")
public class FileController {
@Autowired
private OSS ossClient;
@Value("${spring.aliyun.oss.bucket-name}")
private String bucketName;
@GetMapping("/{fileName}")
public ResponseEntity<InputStreamResource> downloadFile(@PathVariable String fileName) throws Exception {
// 构造OSS文件下载请求
GetObjectRequest request = new GetObjectRequest(bucketName, fileName);
// 下载文件流
InputStream inputStream = ossClient.getObject(request).getObjectContent();
// 设置文件下载响应头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", URLEncoder.encode(fileName, "UTF-8"));
// 返回文件下载响应
return ResponseEntity
.ok()
.headers(headers)
.body(new InputStreamResource(inputStream));
}
}
```
在上述代码中,`/{fileName}`表示下载文件的URL路径,`fileName`是要下载的文件名。
注意:上述代码中的`ossClient`对象需要通过Spring Bean配置来创建,并且需要在应用启动时初始化。
4. 配置OSS客户端的Bean。在你的Spring Boot应用的配置类中,添加以下内容:
```java
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OSSConfig {
@Value("${spring.aliyun.oss.endpoint}")
private String endpoint;
@Value("${spring.aliyun.oss.access-key}")
private String accessKey;
@Value("${spring.aliyun.oss.secret-key}")
private String secretKey;
@Bean
public OSS ossClient() {
return new OSSClientBuilder().build(endpoint, accessKey, secretKey);
}
}
```
在上述配置类中,通过`@Value`注解获取配置文件中的OSS相关配置项,然后创建并返回一个`OSS`对象。
这样,当访问`/files/{fileName}`接口时,就会触发文件下载操作。
需要注意的是,上述代码中的配置项需要根据你自己的阿里云OSS账号进行相应的配置。另外,还需要确保你的Spring Boot应用能够访问到正确的OSS服务。
希望以上信息能够帮助到你实现Spring Boot中的OSS文件下载功能!