12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package org.example.music.controller;
- import io.minio.GetObjectArgs;
- import io.minio.MinioClient;
- import io.minio.errors.*;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.core.io.ByteArrayResource;
- import org.springframework.core.io.Resource;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.MediaType;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import javax.servlet.http.HttpServletRequest;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.security.InvalidKeyException;
- import java.security.NoSuchAlgorithmException;
- @Api(value = "FileDownloadController", tags = {"下载控制类"})
- @Controller
- @RequestMapping("/download")
- public class FileDownloadController {
- @Autowired
- private MinioClient minioClient;
- @Value("${minio.bucket-name}")
- private String bucketName;
- @ApiOperation(value = "下载")
- @GetMapping("/{fileName}")
- public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
- GetObjectArgs args = GetObjectArgs.builder()
- .bucket(bucketName)
- .object(fileName)
- .build();
- InputStream inputStream = minioClient.getObject(args);
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int bytesRead;
- while ((bytesRead = inputStream.read(buffer)) != -1) {
- outputStream.write(buffer, 0, bytesRead);
- }
- byte[] musicBytes = outputStream.toByteArray();
- // 创建一个ByteArrayResource对象,用于包装字节数组
- ByteArrayResource resource = new ByteArrayResource(musicBytes);
- // 构建响应头
- HttpHeaders headers = new HttpHeaders();
- headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
- // 返回一个 ResponseEntity 对象
- return ResponseEntity.ok()
- .headers(headers)
- .contentLength(musicBytes.length)
- .contentType(MediaType.APPLICATION_OCTET_STREAM)
- .body(resource);
- }
- }
|