diff --git a/src/main/java/com/cultural/heritage/controller/file/FileController.java b/src/main/java/com/cultural/heritage/controller/file/FileController.java index 3b1a812..0534113 100644 --- a/src/main/java/com/cultural/heritage/controller/file/FileController.java +++ b/src/main/java/com/cultural/heritage/controller/file/FileController.java @@ -21,6 +21,7 @@ import org.apache.commons.lang3.RandomStringUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import java.io.*; import java.util.Arrays; @RestController @@ -41,13 +42,13 @@ public class FileController { /** - * Web端文件上传 + * Web端文件上传(obs桶) * @param multipartFile 文件对象 * @param uploadFileRequest 业务类型 * @return PutObjectResult */ @PostMapping("/uploadFile") - @Operation(summary = "Web端文件上传", description = "参数:文件对象(multipartFile), 业务类型(biz),权限:管理员,方法名:uploadFile") + @Operation(summary = "Web端文件上传(obs桶)", description = "参数:文件对象(multipartFile), 业务类型(biz),权限:管理员,方法名:uploadFile") public BaseResponse uploadFile(@RequestPart("file") MultipartFile multipartFile, UploadFileRequest uploadFileRequest, HttpServletRequest request) { // 校验用户是否登录 User loginUser = userService.getLoginUser(request); @@ -67,11 +68,11 @@ public class FileController { /** - * 文件下载接口 + * 文件下载(obs桶) * @param objectKey 具体的文件名(含存储路径) */ @GetMapping("downloadFile") - @Operation(summary = "文件下载", description = "参数:文件对象(具体的文件名(含存储路径)),权限:所有人,方法名:downloadFile") + @Operation(summary = "文件下载(obs桶)", description = "参数:文件对象(具体的文件名(含存储路径)),权限:所有人,方法名:downloadFile") public void downloadFile(@RequestParam String objectKey, HttpServletResponse response) { iHweiYunOBSService.downloadFile(objectKey, response); } @@ -79,6 +80,62 @@ public class FileController { + // 优化:设置一个合理的缓冲区大小 + private static final int BUFFER_SIZE = 8192; // 8 KB + + + /** + * 文件下载(服务器) + * @param filename 文件名 + * @param response 响应体 + * @throws IOException + */ + @GetMapping("/download/{filename}") + @Operation(summary = "文件下载(服务器)", description = "参数:文件名,权限:所有人,方法名:downloadFileFromServe") + public void downloadFileFromServe(@PathVariable String filename, HttpServletResponse response) throws IOException { + // 文件所在的路径 + String filePath = "/www/Fonts/" + filename; + File file = new File(filePath); + + // 检查文件是否存在 + if (!file.exists()) { + response.setStatus(HttpServletResponse.SC_NOT_FOUND); + return; + } + // 设置响应头,告诉浏览器这是一个下载文件 + response.setContentType("application/octet-stream"); // 通用的文件流类型 + response.setHeader("Content-Disposition", "attachment; filename=" + file.getName()); + response.setContentLengthLong(file.length()); // 使用 setContentLengthLong 适应大文件 + + // 设置缓存相关的 HTTP 头 + long lastModified = file.lastModified(); + response.setDateHeader("Last-Modified", lastModified); // 文件最后修改时间 + response.setHeader("Cache-Control", "public, max-age=86400"); // 缓存一天(24小时) + response.setHeader("ETag", String.valueOf(lastModified)); // 使用文件最后修改时间作为 ETag + + // 检查浏览器缓存是否有效 + String ifNoneMatch = response.getHeader("If-None-Match"); + if (ifNoneMatch != null && ifNoneMatch.equals(String.valueOf(lastModified))) { + // 如果 ETag 匹配,表示文件没有变化,返回 304 Not Modified + response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + return; + } + // 使用 BufferedInputStream 和 BufferedOutputStream 提高性能 + try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE); + BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream(), BUFFER_SIZE)) { + + byte[] buffer = new byte[BUFFER_SIZE]; + int bytesRead; + + // 从文件中读取数据并写入响应输出流 + while ((bytesRead = bis.read(buffer)) != -1) { + bos.write(buffer, 0, bytesRead); + } + + bos.flush(); // 确保所有数据都已写入响应 + } + } + @@ -123,4 +180,6 @@ public class FileController { String filename = uuid + "-" + multipartFile.getOriginalFilename(); return String.format("%s/%s", fileUploadBizEnum.getValue(), filename); } + + } diff --git a/src/main/java/com/cultural/heritage/controller/other/BannerController.java b/src/main/java/com/cultural/heritage/controller/other/BannerController.java index fd68390..8c15512 100644 --- a/src/main/java/com/cultural/heritage/controller/other/BannerController.java +++ b/src/main/java/com/cultural/heritage/controller/other/BannerController.java @@ -142,7 +142,7 @@ public class BannerController { * 小程序用户根据类型查看轮播图 * @return 轮播图列表 */ - @GetMapping("/query") + @PostMapping("/query") @Operation(summary = "小程序用户根据类型查看轮播图", description = "参数:无,权限:所有人,方法名:queryBannerByType") public BaseResponse> queryBannerByType(@RequestBody CommonStringRequest commonStringRequest) { if (commonStringRequest == null || StringUtils.isBlank(commonStringRequest.getType())) { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index d722f43..f3cae0b 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -75,11 +75,12 @@ springdoc: server: - port: 9092 -# ssl: -# key-store: classpath:carboner.cn.jks -# key-store-password: 6gsn1hke4m4f7 -# key-store-type: JKS + port: 8888 + ssl: + key-store: classpath:carboner.cn.jks + key-store-password: 6gsn1hke4m4f7 + key-store-type: JKS + servlet: context-path: /api # cookie 30 天过期