作业导出500

This commit is contained in:
lu
2024-09-10 17:10:48 +08:00
parent 45fce7927d
commit 2c967cef0a

View File

@@ -404,49 +404,79 @@ public class CoursePortalApi extends ApiBaseController{
if (totalFileSize > 2L * 1024 * 1024 * 1024) {
return success("您要下载的作业过大,请分批下载或联系管理员!");
}
// 创建压缩文件
String zipFilePath = "/home/www/elearning/upload/saveZip/" + courseName+"【作业】" + ".zip";
createZipFile(map, zipFilePath);
return success(zipFilePath);
// 创建一个临时文件用于存储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 条目
ZipEntry entry = new ZipEntry(e.getKey());
entry.setSize(fileToZip.length());
zos.putNextEntry(entry);
try (FileInputStream fis = new FileInputStream(fileToZip)) {
byte[] buffer = new byte[4096];
int len;
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
// 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 条目
// ZipEntry entry = new ZipEntry(e.getKey());
// entry.setSize(fileToZip.length());
//
// zos.putNextEntry(entry);
//
// try (FileInputStream fis = new FileInputStream(fileToZip)) {
// byte[] buffer = new byte[4096];
// int len;
// while ((len = fis.read(buffer)) > 0) {
// zos.write(buffer, 0, len);
// }
// }
//
// zos.closeEntry();
// }
// }
// // 将临时文件移动到指定位置
// Path source = tempZipFile.toPath();
// //生成uuid
//// String uuid = UUID.randomUUID().toString();
// Path destination = Paths.get("/home/www/elearning/upload/saveZip/" + courseName+"【作业】" + ".zip");
//
// // 确保目标目录存在
// Files.createDirectories(destination.getParent());
//
// // 删除目标文件如果已存在
// if (Files.exists(destination)) {
// Files.delete(destination);
// }
//
// // 移动文件
// Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
//
// // 返回文件路径给前端
// String filePath = destination.toAbsolutePath().toString();
// return success(filePath);
}
private static void createZipFile(Map<String, String> map, String zipFilePath) throws IOException {
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
File file = new File(value);
if (!file.exists()) {
continue;
}
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(key);
zos.putNextEntry(zipEntry);
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
}
}
// 将临时文件移动到指定位置
Path source = tempZipFile.toPath();
//生成uuid
// String uuid = UUID.randomUUID().toString();
Path destination = Paths.get("/home/www/elearning/upload/saveZip/" + courseName+"【作业】" + ".zip");
// 确保目标目录存在
Files.createDirectories(destination.getParent());
// 删除目标文件如果已存在
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){