diff --git a/servers/boe-server-all/src/main/java/com/xboe/module/course/api/CoursePortalApi.java b/servers/boe-server-all/src/main/java/com/xboe/module/course/api/CoursePortalApi.java index 3e93694e..0fc8be1c 100644 --- a/servers/boe-server-all/src/main/java/com/xboe/module/course/api/CoursePortalApi.java +++ b/servers/boe-server-all/src/main/java/com/xboe/module/course/api/CoursePortalApi.java @@ -2,8 +2,13 @@ package com.xboe.module.course.api; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.net.URLEncoder; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -360,7 +365,7 @@ public class CoursePortalApi extends ApiBaseController{ } //作业导出 @GetMapping("/export") - public JsonResponse export(String courseName,String courseId,String contentId,String name,Integer status,HttpServletResponse response) throws IOException { + public JsonResponse export(String courseName,String courseId,String contentId,String name,Integer status) throws IOException { Mapmap=new HashMap<>(); List userIds = studyCourseDao.findList(FieldFilters.eq("courseId", courseId)).stream().filter(Objects::nonNull).map(StudyCourse::getAid).collect(Collectors.toList()); if (userIds.isEmpty()){ @@ -400,20 +405,23 @@ public class CoursePortalApi extends ApiBaseController{ return success("您要下载的作业过大,请分批下载或联系管理员!"); } - String encodedFilename = URLEncoder.encode(courseName+"【作业】.zip") - .replace("+", "%20") // 空格替换为"%20" - .replace("%2F", "/"); // 解决斜杠问题 - StringBuilder contentDispositionValue = new StringBuilder(); - contentDispositionValue.append("attachment; filename=\"") - .append(encodedFilename) - .append("\"") - .append("; filename*=utf-8''") - .append(encodedFilename); +// String encodedFilename = URLEncoder.encode(courseName+"【作业】.zip") +// .replace("+", "%20") // 空格替换为"%20" +// .replace("%2F", "/"); // 解决斜杠问题 +// StringBuilder contentDispositionValue = new StringBuilder(); +// contentDispositionValue.append("attachment; filename=\"") +// .append(encodedFilename) +// .append("\"") +// .append("; filename*=utf-8''") +// .append(encodedFilename); +// +// // 设置响应类型和Content-Disposition头 +// response.setContentType("application/zip"); - // 设置响应类型和Content-Disposition头 - response.setContentType("application/zip"); - response.setHeader("Content-Disposition", contentDispositionValue.toString()); - try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) { + // 创建一个临时文件用于存储ZIP文件 + File tempZipFile = new File("/home/www/elearning/upload/temp.zip"); + + try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tempZipFile))) { for (Map.Entry e : map.entrySet()) { File fileToZip = new File(e.getValue()); // 添加 ZIP 条目 @@ -433,7 +441,21 @@ public class CoursePortalApi extends ApiBaseController{ zos.closeEntry(); } } - return success("导出成功"); + // 将临时文件移动到指定位置 + Path source = tempZipFile.toPath(); + Path destination = Paths.get("/home/www/elearning/upload/saveZip" + courseName + "【作业】.zip"); + + // 删除目标文件如果已存在 + if (Files.exists(destination)) { + Files.delete(destination); + } + + // 移动文件 + Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); + + // 返回文件路径给前端 + String filePath = destination.toAbsolutePath().toString(); + return success(filePath); } @GetMapping("/detail-study") public JsonResponse> detailStudy(String courseId, String aid){