From e3a1f75da2fbafaab52b212d51477cbd08c5c7f4 Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Sun, 13 Oct 2024 02:41:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E4=BF=AE=E6=94=B9):=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../services/controller/FilesController.java | 14 +++-- .../security/config/WebSecurityConfig.java | 1 + .../bunny/services/service/FilesService.java | 16 ++++-- .../service/impl/FilesServiceImpl.java | 53 +++++++++++++------ .../service/impl/FilesServiceImplTest.java | 9 ++-- 5 files changed, 66 insertions(+), 27 deletions(-) diff --git a/service/src/main/java/cn/bunny/services/controller/FilesController.java b/service/src/main/java/cn/bunny/services/controller/FilesController.java index 6866d58..0e4a753 100644 --- a/service/src/main/java/cn/bunny/services/controller/FilesController.java +++ b/service/src/main/java/cn/bunny/services/controller/FilesController.java @@ -15,9 +15,9 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.servlet.http.HttpServletResponse; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Mono; @@ -53,9 +53,15 @@ public class FilesController { } @Operation(summary = "下载文件", description = "下载文件") - @GetMapping("downloadFiles/{fileId}") - public void downloadFiles(@PathVariable Long fileId, HttpServletResponse response) { - filesService.downloadFiles(response, fileId); + @GetMapping("downloadFilesByFileId/{fileId}") + public ResponseEntity downloadFilesByFileId(@PathVariable Long fileId) { + return filesService.downloadFilesByFileId(fileId); + } + + @Operation(summary = "根据文件名下载文件", description = "根据文件名下载文件") + @GetMapping("downloadFilesByFilepath") + public ResponseEntity downloadFilesByFilepath(String filepath) { + return filesService.downloadFilesByFilepath(filepath); } @Operation(summary = "更新系统文件表", description = "更新系统文件表") diff --git a/service/src/main/java/cn/bunny/services/security/config/WebSecurityConfig.java b/service/src/main/java/cn/bunny/services/security/config/WebSecurityConfig.java index 550dcfd..785e5a8 100644 --- a/service/src/main/java/cn/bunny/services/security/config/WebSecurityConfig.java +++ b/service/src/main/java/cn/bunny/services/security/config/WebSecurityConfig.java @@ -83,6 +83,7 @@ public class WebSecurityConfig { "/", "/ws/**", "/*/*/noAuth/**", "/*/noAuth/**", "/noAuth/**", "/media.ico", "/favicon.ico", "*.html", "/webjars/**", "/v3/api-docs/**", "swagger-ui/**", + "/*/files/**", "/error", "/*/i18n/getI18n", }; return web -> web.ignoring().requestMatchers(annotations) diff --git a/service/src/main/java/cn/bunny/services/service/FilesService.java b/service/src/main/java/cn/bunny/services/service/FilesService.java index 8b255b1..1a5730a 100644 --- a/service/src/main/java/cn/bunny/services/service/FilesService.java +++ b/service/src/main/java/cn/bunny/services/service/FilesService.java @@ -10,8 +10,8 @@ import cn.bunny.dao.vo.system.files.FileInfoVo; import cn.bunny.dao.vo.system.files.FilesVo; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.IService; -import jakarta.servlet.http.HttpServletResponse; import jakarta.validation.Valid; +import org.springframework.http.ResponseEntity; import java.util.List; @@ -64,8 +64,16 @@ public interface FilesService extends IService { /** * * 下载文件 * - * @param fileId 文件名 - * @param response response + * @param fileId 文件id + * @return 文件字节数组 */ - void downloadFiles(HttpServletResponse response, Long fileId); + ResponseEntity downloadFilesByFileId(Long fileId); + + /** + * * 下载文件 + * + * @param filepath 文件路径 + * @return 文件字节数组 + */ + ResponseEntity downloadFilesByFilepath(String filepath); } diff --git a/service/src/main/java/cn/bunny/services/service/impl/FilesServiceImpl.java b/service/src/main/java/cn/bunny/services/service/impl/FilesServiceImpl.java index a416765..58c47df 100644 --- a/service/src/main/java/cn/bunny/services/service/impl/FilesServiceImpl.java +++ b/service/src/main/java/cn/bunny/services/service/impl/FilesServiceImpl.java @@ -18,15 +18,16 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import jakarta.servlet.http.HttpServletResponse; import jakarta.validation.Valid; import lombok.SneakyThrows; import org.springframework.beans.BeanUtils; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import java.io.IOException; -import java.io.OutputStream; import java.util.List; /** @@ -129,11 +130,11 @@ public class FilesServiceImpl extends ServiceImpl implements /** * * 下载文件 * - * @param response response - * @param fileId 文件名 + * @param fileId 文件id + * @return 文件字节数组 */ @Override - public void downloadFiles(HttpServletResponse response, Long fileId) { + public ResponseEntity downloadFilesByFileId(Long fileId) { // 查询数据库文件信息 Files files = getOne(Wrappers.lambdaQuery().eq(Files::getId, fileId)); @@ -144,18 +145,38 @@ public class FilesServiceImpl extends ServiceImpl implements String filepath = files.getFilepath(); int end = filepath.indexOf("/", 1); filepath = filepath.substring(end + 1); - byte[] buffer = minioUtil.getBucketObjectByte(filepath); + byte[] bytes = minioUtil.getBucketObjectByte(filepath); // 设置响应头 - response.setContentType("application/octet-stream"); - response.setHeader("Content-Disposition", "attachment; filename=\"" + files.getFilename() + "\""); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); + headers.setContentDispositionFormData("attachment", files.getFilename()); - // 写入字节数组到输出流 - try (OutputStream os = response.getOutputStream()) { - os.write(buffer); - os.flush(); - } catch (IOException exception) { - throw new BunnyException(exception.getMessage()); - } + return new ResponseEntity<>(bytes, headers, HttpStatus.OK); + } + + /** + * * 下载文件 + * + * @param filepath 文件路径 + * @return 文件字节数组 + */ + @Override + public ResponseEntity downloadFilesByFilepath(String filepath) { + // 截取文件路径 + int start = filepath.indexOf("/", 1); + filepath = filepath.substring(start + 1); + byte[] bytes = minioUtil.getBucketObjectByte(filepath); + + // 设置文件名称 + int end = filepath.lastIndexOf("/"); + String filename = filepath.substring(end + 1); + + // 设置响应头 + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); + headers.setContentDispositionFormData("attachment", filename); + + return new ResponseEntity<>(bytes, headers, HttpStatus.OK); } } diff --git a/service/src/test/java/cn/bunny/services/service/impl/FilesServiceImplTest.java b/service/src/test/java/cn/bunny/services/service/impl/FilesServiceImplTest.java index da0e303..0a3220e 100644 --- a/service/src/test/java/cn/bunny/services/service/impl/FilesServiceImplTest.java +++ b/service/src/test/java/cn/bunny/services/service/impl/FilesServiceImplTest.java @@ -6,10 +6,13 @@ class FilesServiceImplTest { @Test void stringTest() { String filepath = "/auth-admin/avatar/2024/10-04/5a56ad8f-4468-4780-8a61-424e7de54e04.png"; - int end = filepath.indexOf("/", 1); - - filepath = filepath.substring(end + 1); + int start = filepath.indexOf("/", 1); + filepath = filepath.substring(start + 1); System.out.println(filepath); + + int end = filepath.lastIndexOf("/"); + String filename = filepath.substring(end + 1); + System.out.println(filename); } } \ No newline at end of file