فهرست منبع

fix:bug
1.考生导入zip解密

caozixuan 1 سال پیش
والد
کامیت
92257aeb15

+ 24 - 15
src/main/java/cn/com/qmth/print/manage/service/impl/ExamStudentServiceImpl.java

@@ -28,6 +28,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.core.exception.StatusException;
+import com.qmth.boot.tools.crypto.AES;
+import com.qmth.boot.tools.models.ByteArray;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
@@ -39,6 +41,7 @@ import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
 import java.io.File;
 import java.io.IOException;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.*;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
@@ -186,25 +189,28 @@ public class ExamStudentServiceImpl extends ServiceImpl<ExamStudentDao, ExamStud
 
 
     @Transactional
     @Transactional
     @Override
     @Override
-    public Object analyzeZipAndImportStudents(Long examId, MultipartFile file) throws IOException {
-        String tempPath = sysProperty.getTempDir() + "/examStudent";
+    public Object analyzeZipAndImportStudents(Long examId, MultipartFile file) {
+        String tempPath = sysProperty.getTempDir() + "/importExamStudent";
         try {
         try {
-            File tempZipFile = new File(tempPath + "/examStudent.zip");
-            if (!tempZipFile.exists()) {
-                tempZipFile.mkdirs();
-            }
-            file.transferTo(tempZipFile);
-            String zipPath = tempZipFile.getPath();
-            String dirPath = tempPath + "/examStudent";
+            byte[] bytes = file.getBytes();
+            byte[] keyArr = Arrays.copyOfRange(bytes, 0, 16);
+            byte[] ivArr = Arrays.copyOfRange(bytes, 16, 32);
+            byte[] contentArr = Arrays.copyOfRange(bytes, 32, bytes.length);
+            String key = new String(keyArr, StandardCharsets.UTF_8);
+            String iv = new String(ivArr, StandardCharsets.UTF_8);
+            ByteArray byteArray = AES.decrypt(contentArr, key, iv);
 
 
-            Zip4jUtil.unzipEncryptFile(zipPath, dirPath, "qmth!@#");
+            File decrypt = new File(tempPath + "/decrypt.zip");
+            if (!decrypt.getParentFile().exists()) {
+                decrypt.getParentFile().mkdirs();
+            }
+            byteArray.toFile(decrypt);
+            String dirPath = tempPath + "/unzip";
+            Zip4jUtil.unzip(decrypt.getPath(), dirPath);
 
 
             File zipDir = new File(dirPath);
             File zipDir = new File(dirPath);
             File[] firstDirArr = zipDir.listFiles();
             File[] firstDirArr = zipDir.listFiles();
-            if (Objects.isNull(firstDirArr) || firstDirArr.length != 1) {
-                throw new StatusException("文件解析失败:未找到一级目录");
-            }
-            List<File> studentFileList = Arrays.stream(Objects.requireNonNull(firstDirArr[0].listFiles()))
+            List<File> studentFileList = Arrays.stream(Objects.requireNonNull(firstDirArr))
                     .filter(e -> e.getName().endsWith("xlsx"))
                     .filter(e -> e.getName().endsWith("xlsx"))
                     .collect(Collectors.toList());
                     .collect(Collectors.toList());
             if (CollectionUtils.isEmpty(studentFileList)) {
             if (CollectionUtils.isEmpty(studentFileList)) {
@@ -254,6 +260,9 @@ public class ExamStudentServiceImpl extends ServiceImpl<ExamStudentDao, ExamStud
                 insertData(examEntity, studentList);
                 insertData(examEntity, studentList);
             }
             }
             return excelErrors;
             return excelErrors;
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new StatusException(e.getMessage());
         } finally {
         } finally {
             FileUtil.del(tempPath);
             FileUtil.del(tempPath);
         }
         }
@@ -347,4 +356,4 @@ public class ExamStudentServiceImpl extends ServiceImpl<ExamStudentDao, ExamStud
         }
         }
         return sj.toString();
         return sj.toString();
     }
     }
-}
+}

+ 16 - 0
src/main/java/cn/com/qmth/print/manage/utils/Zip4jUtil.java

@@ -14,6 +14,22 @@ import java.util.Objects;
  */
  */
 public class Zip4jUtil {
 public class Zip4jUtil {
 
 
+    /**
+     * 解压zip
+     *
+     * @param scrPath  zip路径
+     * @param destPath 解压后路径
+     */
+    public static void unzip(String scrPath, String destPath) {
+        if (scrPath == null || scrPath.length() == 0) {
+            throw new StatusException("原始路径不能为空");
+        }
+        if (destPath == null || destPath.length() == 0) {
+            throw new StatusException("解压路径不能为空");
+        }
+        commonUnZipFile(scrPath, destPath, null);
+    }
+
     /**
     /**
      * 解压zip带密码
      * 解压zip带密码
      *
      *