导出判断

This commit is contained in:
lu
2024-08-05 13:57:18 +08:00
parent 389dff8fbe
commit bcb453b2f4

View File

@@ -19,9 +19,7 @@ import com.xboe.core.orm.FieldFilters;
import com.xboe.module.course.entity.*;
import com.xboe.module.course.vo.TeacherVo;
import com.xboe.school.study.dao.StudyCourseDao;
import com.xboe.school.study.dao.StudySignupDao;
import com.xboe.school.study.entity.StudyHomeWork;
import com.xboe.school.study.entity.StudySignup;
import com.xboe.school.study.service.IStudyHomeWorkService;
import com.xboe.school.study.service.IStudyService;
import com.xboe.system.user.entity.User;
@@ -394,6 +392,14 @@ public class CoursePortalApi extends ApiBaseController{
e.printStackTrace();
throw new RuntimeException("导出异常"+e.getMessage());
}
long totalFileSize = map.values().stream()
.mapToLong(path -> new File(path).length())
.sum();
// 检查文件总大小是否超过 2GB
if (totalFileSize > 2L * 1024 * 1024 * 1024) {
return success("您要下载的作业过大,请分批下载或联系管理员!");
}
String encodedFilename = URLEncoder.encode(courseName+"【作业】.zip")
.replace("+", "%20") // 空格替换为"%20"
.replace("%2F", "/"); // 解决斜杠问题
@@ -407,15 +413,9 @@ public class CoursePortalApi extends ApiBaseController{
// 设置响应类型和Content-Disposition头
response.setContentType("application/zip");
response.setHeader("Content-Disposition", contentDispositionValue.toString());
long totalCompressedSize = 0;
try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
for (Map.Entry<String, String> e : map.entrySet()) {
File fileToZip = new File(e.getValue());
// 检查加上当前文件大小后是否会超过2GB
long fileSizeInBytes = fileToZip.length();
if (totalCompressedSize + fileSizeInBytes > 2L * 1024 * 1024 * 1024) {
return success("您要下载的作业过大,联系管理员!");
}
// 添加 ZIP 条目
ZipEntry entry = new ZipEntry(e.getKey());
entry.setSize(fileToZip.length());
@@ -427,7 +427,6 @@ public class CoursePortalApi extends ApiBaseController{
int len;
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
totalCompressedSize += len;
}
}