Prechádzať zdrojové kódy

导入本地路径修改

wangliang 4 rokov pred
rodič
commit
9fb4fb9eb5

+ 21 - 5
themis-business/src/main/java/com/qmth/themis/business/templete/TaskImportCommon.java

@@ -94,6 +94,26 @@ public class TaskImportCommon {
         map.put("timeFormat", timeFormat);
     }
 
+    /**
+     * 获取上传的文件
+     *
+     * @return
+     */
+    public InputStream getUploadFileInputStream() throws IOException {
+        File file = null;
+        StringJoiner localPath = new StringJoiner("").add(SystemConstant.FILES_DIR).add(File.separator)
+                .add(this.path);
+        InputStream inputStream = null;
+        if (Objects.nonNull(this.type) && Objects.equals(this.type, SystemConstant.LOCAL)) {
+            file = new File(localPath.toString());
+            inputStream = new FileInputStream(file);
+        } else {
+            byte[] data = this.ossUtil.download(false, this.path);
+            inputStream = new ByteArrayInputStream(data);
+        }
+        return inputStream;
+    }
+
     /**
      * 获取上传的文件
      *
@@ -115,10 +135,9 @@ public class TaskImportCommon {
      * 写入txt文件
      *
      * @param txtStr
-     * @param excelFile
      * @throws IOException
      */
-    public void writeImportResultTxt(String txtStr, File excelFile) throws IOException {
+    public void writeImportResultTxt(String txtStr) throws IOException {
         ByteArrayOutputStream out = null;
         InputStream inputStream = null;
         try {
@@ -149,9 +168,6 @@ public class TaskImportCommon {
                 out.flush();
                 out.close();
             }
-            if (Objects.nonNull(excelFile)) {
-                excelFile.delete();
-            }
         }
     }
 

+ 3 - 3
themis-business/src/main/java/com/qmth/themis/business/templete/impl/TaskExamPaperImportTemplete.java

@@ -105,7 +105,7 @@ public class TaskExamPaperImportTemplete implements TaskImportTemplete {
             taskImportCommon.getTbTaskHistory().setFinishTime(System.currentTimeMillis());
             taskImportCommon.getTbTaskHistory().setStatus(TaskStatusEnum.FINISH);
             taskImportCommon.getTbTaskHistory().setProgress(100.0d);
-            taskImportCommon.writeImportResultTxt(result.toString(), file);
+            taskImportCommon.writeImportResultTxt(result.toString());
         }
         return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
     }
@@ -232,7 +232,7 @@ public class TaskExamPaperImportTemplete implements TaskImportTemplete {
     }
 
     private void disposePaperDir(String rootDir, TEExam teExam, TEExamCourse course, File paperDir,
-            Map<String, Object> map) throws IOException {
+                                 Map<String, Object> map) throws IOException {
         String paperCode = paperDir.getName();
         File[] childs = paperDir.listFiles();
 
@@ -640,7 +640,7 @@ public class TaskExamPaperImportTemplete implements TaskImportTemplete {
     }
 
     private void checkAnswerFile(String rootDir, File[] paperDirChilds, Long examId, String courseCode,
-            String paperCode) {
+                                 String paperCode) {
         File paperFile = null;
         File answerFile = null;
         for (File cfile : paperDirChilds) {// 校验试卷下的答案

+ 8 - 6
themis-business/src/main/java/com/qmth/themis/business/templete/impl/TaskExamStudentImportTemplete.java

@@ -24,9 +24,8 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.LinkedMultiValueMap;
 
 import javax.annotation.Resource;
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.*;
 
 /**
@@ -56,14 +55,14 @@ public class TaskExamStudentImportTemplete implements TaskImportTemplete {
         log.info("importTask ossMap:{}", JacksonUtil.parseJson(map));
         TaskImportCommon taskImportCommon = new TaskImportCommon(map);
         taskImportCommon.init();
-        File file = taskImportCommon.getUploadFile();
+        InputStream inputStream = taskImportCommon.getUploadFileInputStream();
         taskImportCommon.setTxtList(new ArrayList());
         String timeFormat = taskImportCommon.getTimeFormat();
         taskImportCommon.getTxtList().add(DateUtil.format(new Date(), timeFormat) + "->开始准备处理导入的考生数据");
         Map<String, Object> finalMap = map;
         Map<String, TEExamActivity> teExamActivityMap = (Map<String, TEExamActivity>) map.get("teExamActivityMap");
         try {
-            List<LinkedMultiValueMap<Integer, Object>> finalList = ExcelUtil.excelReader(new FileInputStream(file), Lists.newArrayList(ExamStudentImportDto.class), new ExcelCallback() {
+            List<LinkedMultiValueMap<Integer, Object>> finalList = ExcelUtil.excelReader(inputStream, Lists.newArrayList(ExamStudentImportDto.class), new ExcelCallback() {
                 @Override
                 public List<LinkedMultiValueMap<Integer, Object>> callback(List<LinkedMultiValueMap<Integer, Object>> finalList, List<LinkedMultiValueMap<Integer, String>> finalColumnNameList) throws IllegalAccessException, IOException {
                     Map<String, String> courseCodeMap = new HashMap<>();
@@ -97,7 +96,7 @@ public class TaskExamStudentImportTemplete implements TaskImportTemplete {
                         taskImportCommon.getTbTaskHistory().setProgress(0d);
                         taskImportCommon.getTbTaskHistory().setStatus(TaskStatusEnum.FINISH);
                         taskImportCommon.getTbTaskHistory().setSummary(taskImportCommon.getTxtList().toString());
-                        taskImportCommon.writeImportResultTxt(taskImportCommon.getTxtList().toString(), file);
+                        taskImportCommon.writeImportResultTxt(taskImportCommon.getTxtList().toString());
                         throw new BusinessException(JSONObject.toJSONString(excelErrorList));
                     }
                     finalMap.put("courseCodeMap", courseCodeMap);
@@ -118,8 +117,11 @@ public class TaskExamStudentImportTemplete implements TaskImportTemplete {
             int min = Objects.isNull(map.get("min")) ? 0 : Integer.parseInt(String.valueOf(map.get("min")));
             taskImportCommon.exception(min, e, taskImportCommon.getTxtList());
         } finally {
+            if (Objects.nonNull(inputStream)) {
+                inputStream.close();
+            }
             //这里写入txt文件
-            taskImportCommon.writeImportResultTxt(taskImportCommon.getTxtList().toString(), file);
+            taskImportCommon.writeImportResultTxt(taskImportCommon.getTxtList().toString());
         }
         return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
     }

+ 8 - 6
themis-business/src/main/java/com/qmth/themis/business/templete/impl/TaskRoomCodeImportTemplete.java

@@ -21,9 +21,8 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.LinkedMultiValueMap;
 
 import javax.annotation.Resource;
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.*;
 
 /**
@@ -52,12 +51,12 @@ public class TaskRoomCodeImportTemplete implements TaskImportTemplete {
     public Result importTask(Map<String, Object> map) throws IOException {
         TaskImportCommon taskImportCommon = new TaskImportCommon(map);
         taskImportCommon.init();
-        File file = taskImportCommon.getUploadFile();
+        InputStream inputStream = taskImportCommon.getUploadFileInputStream();
         taskImportCommon.setTxtList(new ArrayList());
         String timeFormat = taskImportCommon.getTimeFormat();
         taskImportCommon.getTxtList().add(DateUtil.format(new Date(), timeFormat) + "->开始准备处理导入的考场数据");
         try {
-            List<LinkedMultiValueMap<Integer, Object>> finalList = ExcelUtil.excelReader(new FileInputStream(file), Lists.newArrayList(RoomCodeImportDto.class), new ExcelCallback() {
+            List<LinkedMultiValueMap<Integer, Object>> finalList = ExcelUtil.excelReader(inputStream, Lists.newArrayList(RoomCodeImportDto.class), new ExcelCallback() {
                 @Override
                 public List<LinkedMultiValueMap<Integer, Object>> callback(List<LinkedMultiValueMap<Integer, Object>> finalList, List<LinkedMultiValueMap<Integer, String>> finalColumnNameList) throws IllegalAccessException, IOException {
                     List<ExcelError> excelErrorList = new ArrayList<>();
@@ -79,7 +78,7 @@ public class TaskRoomCodeImportTemplete implements TaskImportTemplete {
                         taskImportCommon.getTbTaskHistory().setProgress(0d);
                         taskImportCommon.getTbTaskHistory().setStatus(TaskStatusEnum.FINISH);
                         taskImportCommon.getTbTaskHistory().setSummary(taskImportCommon.getTxtList().toString());
-                        taskImportCommon.writeImportResultTxt(taskImportCommon.getTxtList().toString(), file);
+                        taskImportCommon.writeImportResultTxt(taskImportCommon.getTxtList().toString());
                         throw new BusinessException(JSONObject.toJSONString(excelErrorList));
                     }
                     return finalList;
@@ -98,8 +97,11 @@ public class TaskRoomCodeImportTemplete implements TaskImportTemplete {
             int min = Objects.isNull(map.get("min")) ? 0 : Integer.parseInt(String.valueOf(map.get("min")));
             taskImportCommon.exception(min, e, taskImportCommon.getTxtList());
         } finally {
+            if (Objects.nonNull(inputStream)) {
+                inputStream.close();
+            }
             //这里写入txt文件
-            taskImportCommon.writeImportResultTxt(taskImportCommon.getTxtList().toString(), file);
+            taskImportCommon.writeImportResultTxt(taskImportCommon.getTxtList().toString());
         }
         return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
     }