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 90a821c..511131c 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,8 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.*; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Arrays; @RestController @@ -41,9 +43,11 @@ public class FileController { // 优化:设置一个合理的缓冲区大小 private static final int BUFFER_SIZE = 8192; // 8 KB - // 上传文件的服务器存储目录 - private static final String UPLOAD_DIR = "/www/wwwroot/fileUpload/"; + // 上传文件的服务器存储目录 + private static final String UPLOAD_DIR = "/www/wwwroot/"; + +// // // 上传文件的本地存储目录 // private static final String UPLOAD_DIR = "D:/uploads/fileUpload/"; @@ -82,7 +86,7 @@ public class FileController { // public void downloadFile(@RequestParam String objectKey, HttpServletResponse response) { // iHweiYunOBSService.downloadFile(objectKey, response); // } -// + @@ -97,21 +101,16 @@ public class FileController { @PostMapping("/upload") @Operation(summary = "Web端文件上传(服务器)", description = "参数:文件对象(multipartFile), 业务类型(biz),权限:管理员,方法名:uploadFile") public BaseResponse uploadFileToServe(@RequestPart("file") MultipartFile multipartFile, UploadFileRequest uploadFileRequest, HttpServletRequest request) throws IOException { - // 校验用户是否登录 userService.getLoginUser(request); - // 校验文件 validFile(multipartFile); - // 获取文件的保存路径 String filePath = getFilePath(multipartFile, uploadFileRequest); - // 格式化图片特殊字符 filePath = RegexUtils.encodeUrl(filePath); - // 创建上传目录,如果不存在 - File file = new File(UPLOAD_DIR + filePath); + File file = new File(UPLOAD_DIR + "feiyi/img/" + filePath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs();// 如果路径不存在则创建 } @@ -121,7 +120,6 @@ public class FileController { } catch (IOException e) { throw new BusinessException(ErrorCode.OPERATION_ERROR, "文件上传失败,失败原因:" + e.getMessage()); } - return ResultUtils.success(filePath, "上传成功"); } @@ -133,48 +131,32 @@ public class FileController { * @param filename 文件名 * @throws IOException */ - @GetMapping("/download/{filename}") + @GetMapping("/downloadFile") @Operation(summary = "文件下载(服务器)", description = "参数:文件名,权限:所有人,方法名:downloadFileFromServe") - public void downloadFileFromServe(@PathVariable String filename, HttpServletResponse response) throws IOException { + public void downloadFileFromServe(@RequestParam("objectKey") String filename, HttpServletResponse response) throws IOException { // 文件所在的路径 - String filePath = UPLOAD_DIR + filename; + String filePath = null; + if (filename.contains("/")) { + filePath = UPLOAD_DIR + filename; + } else { + filePath = UPLOAD_DIR + "feiyi/img/" + 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; - } + // 清空response + response.reset(); + // 设置response的Header + response.setContentType("application/octet-stream"); + response.addHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(file.getName(), StandardCharsets.UTF_8)); + response.setCharacterEncoding("UTF-8"); // 使用 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(); // 确保所有数据都已写入响应 } } @@ -182,6 +164,42 @@ public class FileController { + /** + * 字体资源下载(服务器) + * @param filename 文件名 + * @throws IOException + */ + @GetMapping("/download/{objectKey}") + @Operation(summary = "字体资源下载(服务器)", description = "参数:文件名,权限:所有人,方法名:downloadFontFromServe") + public void downloadFontFromServe(@PathVariable("objectKey") String filename, HttpServletResponse response) throws IOException { + // 文件所在的路径 + filename = "feiyi/fonts/" + filename; // 字体路径 + String filePath = UPLOAD_DIR + filename; + File file = new File(filePath); + // 清空response + response.reset(); + // 设置response的Header + response.setContentType("application/octet-stream"); + response.addHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(file.getName(), StandardCharsets.UTF_8)); + response.setCharacterEncoding("UTF-8"); + // 使用 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(); // 确保所有数据都已写入响应 + } + } + + + + + + /** @@ -220,7 +238,7 @@ public class FileController { // 校验文件 validFile(multipartFile); // 文件目录:根据业务、用户来划分 - String uuid = RandomStringUtils.randomAlphabetic(8); + String uuid = RandomStringUtils.randomAlphabetic(16); String filename = uuid + "-" + multipartFile.getOriginalFilename(); return String.format("%s/%s", fileUploadBizEnum.getValue(), filename); } diff --git a/src/main/java/com/cultural/heritage/controller/wx/WeChatPayController.java b/src/main/java/com/cultural/heritage/controller/wx/WeChatPayController.java index 432c185..dd8c9cf 100644 --- a/src/main/java/com/cultural/heritage/controller/wx/WeChatPayController.java +++ b/src/main/java/com/cultural/heritage/controller/wx/WeChatPayController.java @@ -235,7 +235,6 @@ public class WeChatPayController { Long orderId = commonRequest.getId(); Order order = ordersService.getById(orderId); ThrowUtils.throwIf(order == null, ErrorCode.OPERATION_ERROR, "订单不存在"); - Refund refund = weChatService.refundRestPayment(String.valueOf(orderId), order.getTotalAmount()); return ResultUtils.success(refund); }