|
|
@@ -19,11 +19,14 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.lc.ibps.base.core.util.*;
|
|
|
+import com.lc.ibps.base.core.util.Collections;
|
|
|
import com.lc.ibps.base.framework.id.UniqueIdUtil;
|
|
|
import com.lc.ibps.base.framework.table.ICommonDao;
|
|
|
import com.lc.ibps.cloud.redis.utils.RedisUtil;
|
|
|
import com.lc.ibps.common.file.persistence.entity.AttachmentPo;
|
|
|
import com.lc.ibps.file.server.model.FolderNodeInfo;
|
|
|
+import com.lc.ibps.file.server.model.ZipFileEntry;
|
|
|
+import com.lc.ibps.file.server.model.ZipProcessResult;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.apache.commons.lang3.RandomStringUtils;
|
|
|
import org.jodconverter.OfficeDocumentConverter;
|
|
|
@@ -179,6 +182,69 @@ public class DownloadProvider extends GenericUploadProvider implements IDownload
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @ApiOperation(value = "zip文件解析", notes = "zip文件解析")
|
|
|
+ @Override
|
|
|
+ public APIResult<Map<String, Object>> unCalibrationZip(String attachmentId) {
|
|
|
+ APIResult<Map<String, Object>> result = new APIResult<>();
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ try{
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(attachmentId)) {
|
|
|
+ result.setState(StateEnum.ERROR.getCode());
|
|
|
+ result.setMessage("zip附件id不能为空");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ this.getUploadService();
|
|
|
+ FileInfo fileInfo = uploadService.downloadFile(attachmentId);
|
|
|
+ if (BeanUtils.isEmpty(fileInfo)) {
|
|
|
+ result.setState(StateEnum.ERROR.getCode());
|
|
|
+ result.setMessage("zip文件不存在");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!"zip".equals(fileInfo.getExt())) {
|
|
|
+ logger.error("文件不是ZIP格式: {}", fileInfo.getFileName());
|
|
|
+ result.setState(StateEnum.ERROR.getCode());
|
|
|
+ result.setMessage("文件不是ZIP格式,请上传ZIP文件");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ byte[] zipContent = getZipContent(fileInfo);
|
|
|
+ if (zipContent == null || zipContent.length == 0) {
|
|
|
+ logger.error("ZIP文件内容为空,attachmentId: {}", attachmentId);
|
|
|
+ result.setState(StateEnum.ERROR.getCode());
|
|
|
+ result.setMessage("ZIP文件内容为空");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ ZipProcessResult processResult = processZipFile(zipContent);
|
|
|
+
|
|
|
+
|
|
|
+ // 6. 判断处理结果
|
|
|
+ if (!processResult.isSuccess()) {
|
|
|
+ // 有重名文件,返回错误信息
|
|
|
+ result.setState(StateEnum.ERROR.getCode());
|
|
|
+ result.setMessage(processResult.getErrorMessage());
|
|
|
+ // 可选:返回重复的文件名列表供前端展示
|
|
|
+ map.put("duplicateFiles", processResult.getDuplicateFiles());
|
|
|
+
|
|
|
+ result.setData(map);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 7. 处理成功
|
|
|
+ result.setState(StateEnum.SUCCESS.getCode());
|
|
|
+ map.put("data",processResult.getUploadedFileIds());
|
|
|
+ result.setMessage("ZIP文件处理成功");
|
|
|
+ result.setVariables(map);
|
|
|
+ return result;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("处理ZIP文件异常,attachmentId: {}", attachmentId, e);
|
|
|
+ result.setState(StateEnum.ERROR.getCode());
|
|
|
+ result.setMessage("系统处理异常:" + e.getMessage());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private byte[] getZipContent(FileInfo fileInfo) throws IOException {
|
|
|
byte[] zipContent = fileInfo.getFileBytes();
|
|
|
if (zipContent != null && zipContent.length > 0) {
|
|
|
@@ -194,6 +260,133 @@ public class DownloadProvider extends GenericUploadProvider implements IDownload
|
|
|
return Files.readAllBytes(filePath);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 处理ZIP文件:校验重名并上传
|
|
|
+ */
|
|
|
+ private ZipProcessResult processZipFile(byte[] zipContent) throws Exception {
|
|
|
+ ZipProcessResult processResult = new ZipProcessResult();
|
|
|
+ Set<String> fileNameSet = new HashSet<>();
|
|
|
+ List<String> duplicateList = new ArrayList<>();
|
|
|
+ List<ZipFileEntry> fileEntries = new ArrayList<>();
|
|
|
+
|
|
|
+ try (ByteArrayInputStream bais = new ByteArrayInputStream(zipContent);
|
|
|
+ ZipInputStream zipInputStream = new ZipInputStream(bais, Charset.forName("GBK"))) {
|
|
|
+
|
|
|
+ ZipEntry entry;
|
|
|
+ while ((entry = zipInputStream.getNextEntry()) != null) {
|
|
|
+ String entryName = entry.getName();
|
|
|
+ // 跳过目录条目
|
|
|
+ if (entry.isDirectory()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 提取文件名(不含路径)
|
|
|
+ String fileName = extractFileName(entryName);
|
|
|
+ // 检查文件名是否为空
|
|
|
+ if (StringUtils.isBlank(fileName)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 检查是否有重名文件
|
|
|
+ if (fileNameSet.contains(fileName)) {
|
|
|
+ if (!duplicateList.contains(fileName)) {
|
|
|
+ duplicateList.add(fileName);
|
|
|
+ }
|
|
|
+ processResult.setSuccess(false);
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ fileNameSet.add(fileName);
|
|
|
+ }
|
|
|
+ // 读取文件内容到字节数组
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
+ int len;
|
|
|
+ while ((len = zipInputStream.read(buffer)) > 0) {
|
|
|
+ baos.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ // 保存文件信息(包含字节内容和文件名)
|
|
|
+ ZipFileEntry fileEntry = new ZipFileEntry();
|
|
|
+ fileEntry.setFileName(fileName);
|
|
|
+ fileEntry.setContent(baos.toByteArray());
|
|
|
+ fileEntry.setSize(entry.getSize());
|
|
|
+ fileEntry.setEntryName(entryName);
|
|
|
+ fileEntries.add(fileEntry);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有重名文件,返回错误
|
|
|
+ if (!processResult.isSuccess()) {
|
|
|
+ processResult.setErrorMessage("ZIP文件中存在重名文件,请检查后重新上传");
|
|
|
+ processResult.setDuplicateFiles(duplicateList);
|
|
|
+ return processResult;
|
|
|
+ }
|
|
|
+ // 没有重名文件,执行上传逻辑
|
|
|
+ try {
|
|
|
+ List<Map<String, Object>> fileList = uploadZipFiles(fileEntries);
|
|
|
+ processResult.setSuccess(true);
|
|
|
+ processResult.setTotalFiles(fileEntries.size());
|
|
|
+ processResult.setUploadedFileIds(fileList);
|
|
|
+ processResult.setMessage("上传成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("文件上传失败", e);
|
|
|
+ processResult.setSuccess(false);
|
|
|
+ processResult.setErrorMessage("文件上传失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return processResult;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 提取文件名(去除路径)
|
|
|
+ */
|
|
|
+ private String extractFileName(String entryName) {
|
|
|
+ if (entryName == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ // 处理Windows和Unix路径分隔符
|
|
|
+ int lastSeparator = Math.max(entryName.lastIndexOf('/'), entryName.lastIndexOf('\\'));
|
|
|
+ if (lastSeparator >= 0) {
|
|
|
+ return entryName.substring(lastSeparator + 1);
|
|
|
+ }
|
|
|
+ return entryName;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量上传ZIP中的文件
|
|
|
+ */
|
|
|
+ private List<Map<String,Object>> uploadZipFiles(List<ZipFileEntry> fileEntries) throws Exception {
|
|
|
+ List<Map<String,Object>> uploaded = new ArrayList<>();
|
|
|
+
|
|
|
+ for (ZipFileEntry entry : fileEntries) {
|
|
|
+ // 1. 将字节数组转换为 InputStream
|
|
|
+ try (ByteArrayInputStream bais = new ByteArrayInputStream(entry.getContent())) {
|
|
|
+
|
|
|
+ // 2. 构建上传参数
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("fileName", entry.getFileName());
|
|
|
+ params.put("fileSize", entry.getSize());
|
|
|
+ params.put("originalFilename", entry.getFileName());
|
|
|
+ params.put("curUserId",ContextUtil.getCurrentUser().getUserId());
|
|
|
+ params.put("curAccount",ContextUtil.getCurrentUser().getAccount());
|
|
|
+ params.put("curUserName",ContextUtil.getCurrentUser().getFullname());
|
|
|
+ // 如果有其他参数需要传递,可以继续添加
|
|
|
+ // 3. 调用上传服务
|
|
|
+ FileInfo uploadedFile = uploadService.uploadFile(bais, params);
|
|
|
+ // 4. 记录上传后的文件ID
|
|
|
+ if (uploadedFile != null && StringUtils.isNotBlank(uploadedFile.getId())) {
|
|
|
+ Map<String, Object> hashMap = new HashMap<>();
|
|
|
+ hashMap.put("id",uploadedFile.getId());
|
|
|
+ hashMap.put("name",uploadedFile.getFileName()+"."+uploadedFile.getExt());
|
|
|
+ uploaded.add(hashMap);
|
|
|
+ logger.info("文件上传成功: {}, 文件ID: {}", entry.getFileName(), uploadedFile.getId());
|
|
|
+ } else {
|
|
|
+ logger.warn("文件上传返回为空: {}", entry.getFileName());
|
|
|
+ throw new RuntimeException("文件上传失败:" + entry.getFileName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return uploaded;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
private ZipImportStat importZipToDatabase(byte[] zipContent, String parentFolderId) throws IOException {
|
|
|
ZipImportStat stat = new ZipImportStat();
|