导出逻辑

This commit is contained in:
lu
2024-08-05 18:46:29 +08:00
parent bcb453b2f4
commit 484a7650dd

View File

@@ -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<String> export(String courseName,String courseId,String contentId,String name,Integer status) throws IOException {
Map<String, String>map=new HashMap<>();
List<String> 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<String, String> 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<List<CourseStudyVo>> detailStudy(String courseId, String aid){