java spring boot 从s3下载zip返给前台浏览器,直接浏览器下载
时间: 2024-11-24 17:43:17 浏览: 23
在Java Spring Boot应用中,如果你想要从Amazon S3下载ZIP文件并直接提供给前端浏览器下载,你需要做以下几个步骤:
1. 添加依赖:首先,确保你在项目的pom.xml文件中添加了AWS SDK for Java(S3客户端)以及Spring Web相关的依赖。
```xml
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
```
2. 配置AWS访问密钥和凭证:在application.properties或application.yml文件中设置AWS的凭据,通常包括AWS Access Key ID、Secret Access Key和区域信息。
3. 创建服务类:创建一个负责下载文件的服务类,使用AmazonS3Client对象来获取S3资源。
```java
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class S3FileDownloader {
private AmazonS3 s3Client;
@Value("${s3.bucket.name}")
private String bucketName;
public byte[] downloadFile(String objectKey) {
try {
return s3Client.getObject(new GetObjectRequest(bucketName, objectKey))
.getObjectContent().array();
} catch (Exception e) {
throw new RuntimeException("Failed to download file from S3", e);
}
}
// 构造函数初始化S3Client
public S3FileDownloader() {
this.s3Client = AmazonS3ClientBuilder.defaultClient();
}
}
```
4. 下载处理:在某个控制器类中,接收前端请求,然后调用`downloadFile`方法,并返回一个响应头指示浏览器下载。
```java
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import static org.springframework.http.HttpHeaders.CONTENT_DISPOSITION;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
@RestController
public class FileController {
private final S3FileDownloader downloader;
public FileController(S3FileDownloader downloader) {
this.downloader = downloader;
}
@GetMapping("/downloadZip")
public ResponseEntity<byte[]> downloadZip(@RequestParam("fileKey") String fileKey) {
byte[] fileData = downloader.downloadFile(fileKey);
HttpHeaders headers = new HttpHeaders();
headers.add(CONTENT_DISPOSITION, "attachment; filename=archive.zip");
headers.setContentType(APPLICATION_OCTET_STREAM);
return ResponseEntity.ok()
.headers(headers)
.body(fileData);
}
}
```
5. 浏览器请求:当用户通过URL像`/downloadZip?fileKey=<your-file-key>`发起GET请求时,Spring会处理这个请求,从S3下载文件并通过HTTP响应头告诉浏览器以下载的方式处理。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)