批量下载后文件无法打开,下载内容为空bug修改

This commit is contained in:
liang.chao 2025-05-21 14:32:44 +08:00
parent c74047eebc
commit cd8c0a32e3
3 changed files with 132 additions and 52 deletions

View File

@ -94,6 +94,9 @@ public class CoursewareLibraryController {
stringBuilder.append(line); stringBuilder.append(line);
} }
String str = decord(stringBuilder.toString()); String str = decord(stringBuilder.toString());
if (str.startsWith("[") && str.endsWith("]")) {
str = str.substring(1, str.length() - 1);
}
String courseId = str.split("&")[0].split("=")[1]; String courseId = str.split("&")[0].split("=")[1];
String fileType = str.split("&")[1].split("=")[1]; String fileType = str.split("&")[1].split("=")[1];
if (!StaticVariableUtils.ONE.equals(fileType)) { if (!StaticVariableUtils.ONE.equals(fileType)) {
@ -118,6 +121,7 @@ public class CoursewareLibraryController {
} }
/** /**
* 课件上传 * 课件上传
* *
@ -146,6 +150,7 @@ public class CoursewareLibraryController {
/** /**
* 1.新增题库使用的tree 2.移动使用的tree * 1.新增题库使用的tree 2.移动使用的tree
*
* @param dto * @param dto
* @return AjaxResult * @return AjaxResult
* @author cwchen * @author cwchen

View File

@ -35,6 +35,9 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -148,7 +151,7 @@ public class CoursewareLibraryServiceImpl implements CoursewareLibraryService {
if (Objects.equals(dto.getFileType(), BusinessConstants.FILE_TYPE)) { if (Objects.equals(dto.getFileType(), BusinessConstants.FILE_TYPE)) {
// 多个文件下载 // 多个文件下载
CoursewareVo vo = new CoursewareVo(); CoursewareVo vo = new CoursewareVo();
String[] split = dto.getCourseId().split("%252C"); String[] split = dto.getCourseId().split("%2C");
List<CoursewareVo> list = mapper.getSelectFileToDownload(split); List<CoursewareVo> list = mapper.getSelectFileToDownload(split);
downLoadBatchFile(response, list); downLoadBatchFile(response, list);
@ -217,7 +220,7 @@ public class CoursewareLibraryServiceImpl implements CoursewareLibraryService {
String tempTime = "temp_" + System.currentTimeMillis(); String tempTime = "temp_" + System.currentTimeMillis();
String parentSourceFilePath = upload_path + File.separator + "coursewareZip"; String parentSourceFilePath = upload_path + File.separator + "coursewareZip";
if (!new File(parentSourceFilePath).exists()) { if (!new File(parentSourceFilePath).exists()) {
new File(parentSourceFilePath).mkdir(); new File(parentSourceFilePath).mkdirs();
} }
String sourceFilePath = parentSourceFilePath + File.separator + tempTime; String sourceFilePath = parentSourceFilePath + File.separator + tempTime;
String zipFilePath = null; String zipFilePath = null;
@ -245,13 +248,34 @@ public class CoursewareLibraryServiceImpl implements CoursewareLibraryService {
invokeAllTasks(testTaskExecutor,tasks); invokeAllTasks(testTaskExecutor,tasks);
///usr/local/bonus/uploadPath/tempTime文件夹进行压缩 ///usr/local/bonus/uploadPath/tempTime文件夹进行压缩
// Compress files into a zip // Compress files into a zip
String zipFileName = "courseware_" + tempTime + ".zip"; String zipFileName = tempTime + ".zip";
zipFilePath = parentSourceFilePath + File.separator + zipFileName; zipFilePath = parentSourceFilePath + File.separator + zipFileName;
zipFolderContents(sourceFilePath, zipFilePath); zipFolder(sourceFilePath, zipFilePath);
// Set response headers File zipFile = new File(zipFilePath);
if (!zipFile.exists() || zipFile.isDirectory()) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "文件不存在");
return;
}
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=" +
URLEncoder.encode(zipFileName, "UTF-8"));
response.setContentLength((int) zipFile.length());
try (InputStream is = new FileInputStream(zipFile);
OutputStream os = response.getOutputStream()) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
}
// zipFolderContents(sourceFilePath, zipFilePath);
// Set response headers
/* response.setCharacterEncoding("utf-8");
response.setContentType("application/zip"); response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=" response.setHeader("Content-Disposition", "attachment;filename="
.concat(String.valueOf(URLEncoder.encode(zipFileName, "UTF-8")))); .concat(String.valueOf(URLEncoder.encode(zipFileName, "UTF-8"))));
@ -260,19 +284,39 @@ public class CoursewareLibraryServiceImpl implements CoursewareLibraryService {
try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) { try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
File sourceFolder = new File(sourceFilePath); File sourceFolder = new File(sourceFilePath);
compressFolderContents(sourceFolder, zos, ""); compressFolderContents(sourceFolder, zos, "");
} }*/
} catch (Exception e) { } catch (Exception e) {
log.error(e.toString(), e); log.error(e.toString(), e);
throw new ServiceException("文件下载异常"); throw new ServiceException("文件下载异常");
} finally {
// FileUtil.deleteFolder(Paths.get(sourceFilePath));
// if (zipFilePath != null) {
// FileUtil.deleteFolder(Paths.get(zipFilePath));
// }
} }
} }
public static void zipFolder(String sourceFolderPath, String zipFilePath) throws IOException {
Path sourcePath = Paths.get(sourceFolderPath);
if (!Files.exists(sourcePath) || !Files.isDirectory(sourcePath)) {
throw new IllegalArgumentException("源文件夹不存在或不是目录: " + sourceFolderPath);
}
Path zipPath = Paths.get(zipFilePath);
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipPath))) {
Files.walk(sourcePath)
.filter(path -> !Files.isDirectory(path)) // 只处理文件排除目录本身
.forEach(path -> {
try {
// 计算相对于源文件夹的相对路径
Path targetPath = sourcePath.relativize(path);
ZipEntry zipEntry = new ZipEntry(targetPath.toString().replace("\\", "/"));
zos.putNextEntry(zipEntry);
// 将文件内容写入 ZIP 输出流
Files.copy(path, zos);
zos.closeEntry();
} catch (IOException e) {
System.err.println("无法添加文件到ZIP: " + path + ", 错误信息: " + e.getMessage());
}
});
}
}
public static boolean zipFolderContents(String sourceFolderPath, String zipFilePath) { public static boolean zipFolderContents(String sourceFolderPath, String zipFilePath) {
try { try {
FileOutputStream fos = new FileOutputStream(zipFilePath); FileOutputStream fos = new FileOutputStream(zipFilePath);

View File

@ -103,8 +103,10 @@ public class ProjectServiceImpl implements ProjectService {
@Value("${user.url}") @Value("${user.url}")
private String url; private String url;
/** /**
* 短信内容处理 * 短信内容处理
*
* @param bean 实体 * @param bean 实体
* @return 短信内容 * @return 短信内容
*/ */
@ -249,6 +251,7 @@ public class ProjectServiceImpl implements ProjectService {
} }
return AjaxResult.error("添加总监人员失败,请联系管理员!"); return AjaxResult.error("添加总监人员失败,请联系管理员!");
} }
@Override @Override
public AjaxResult addSupervisoryUnitUser(SupervisoryUnit bean) { public AjaxResult addSupervisoryUnitUser(SupervisoryUnit bean) {
try { try {
@ -274,6 +277,7 @@ public class ProjectServiceImpl implements ProjectService {
/** /**
* 查询工程必填项 * 查询工程必填项
*
* @param entity * @param entity
* @return * @return
*/ */
@ -291,6 +295,7 @@ public class ProjectServiceImpl implements ProjectService {
/** /**
* 新增承包商单位 * 新增承包商单位
*
* @param bean 监理单位实体 * @param bean 监理单位实体
* @return 是否新增成功 * @return 是否新增成功
*/ */
@ -528,6 +533,31 @@ public class ProjectServiceImpl implements ProjectService {
throw new RuntimeException("Failed to add address project"); throw new RuntimeException("Failed to add address project");
} }
} }
//插入承包商-工程关联信息(先删后增---临时先放开 后期可能会删除)
projectMapper.delConsProject(project);
projectMapper.delMaterialProject(project);
projectMapper.delConsPersonToPcp(project);
projectMapper.delConsPersonToLk(project);
for (int i = 0; i < project.getConsArr().size(); i++) {
project.setConsId(projectMapper.getConsUuid(project.getConsArr().get(i).getValue()));
project.setRelateUuid(StringUtils.getUuid());
result = projectMapper.addConsProject(project);
if (result <= 0) {
throw new RuntimeException("Failed to add cons project");
}
for (int j = 0; j < project.getConsArr().get(i).getCheckList().size(); j++) {
//插入材料-工程关联信息
project.setMaterialId(project.getConsArr().get(i).getCheckList().get(j));
project.setRelateUuid(StringUtils.getUuid());
project.setConsId(project.getConsArr().get(i).getValue());
result = projectMapper.addMaterialProject(project);
if (result <= 0) {
throw new RuntimeException("Failed to add material project");
}
}
// TODO 获取承包商信息 --- 新增承包商信息到承包商人员表
addConsPerson(project);
}
} catch (Exception e) { } catch (Exception e) {
result = 0; result = 0;
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
@ -536,7 +566,6 @@ public class ProjectServiceImpl implements ProjectService {
} }
/** /**
* 插入关联信息 * 插入关联信息
* *
@ -588,6 +617,7 @@ public class ProjectServiceImpl implements ProjectService {
} }
return result; return result;
} }
/** /**
* 新增监理人员 * 新增监理人员
* *
@ -603,6 +633,7 @@ public class ProjectServiceImpl implements ProjectService {
//插入关联表 //插入关联表
projectMapper.addSupPersonToLk(subPerson); projectMapper.addSupPersonToLk(subPerson);
} }
/** /**
* 新增承包商人员 * 新增承包商人员
* *