فهرست منبع

为云阅卷添加导出json文件功能

lideyin 5 سال پیش
والد
کامیت
4c1993661e

+ 63 - 61
src/main/java/cn/com/qmth/examcloud/bridge/modules/cloudmarking/controller/CloudMarkingController.java

@@ -8,7 +8,9 @@ import cn.com.qmth.examcloud.core.oe.admin.api.ExamRecordQuestionsCloudService;
 import cn.com.qmth.examcloud.core.oe.admin.api.bean.SubjectiveAnswerBean;
 import cn.com.qmth.examcloud.core.oe.admin.api.request.GetSubjectiveAnswerReq;
 import cn.com.qmth.examcloud.core.oe.admin.api.response.GetSubjectiveAnswerResp;
+import cn.com.qmth.examcloud.web.config.SystemConfig;
 import com.google.common.collect.Maps;
+import org.apache.commons.io.FileUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
@@ -37,6 +39,9 @@ public class CloudMarkingController {
     @Autowired
     ExamRecordQuestionsCloudService examRecordQuestionsCloudService;
 
+    @Autowired
+    SystemConfig systemConfig;
+
     /**
      * 生成主观题答案
      */
@@ -47,75 +52,20 @@ public class CloudMarkingController {
         }
 
         //答案临时存放目录
-        final String tempAnswerDir = "tempAnswer";
-        //答案临时压缩目录
-        final String tempAnswerZipDir = "tempAnswerZip";
-
-        /*//清空临时目录中的文件
-        File t1 = new File(tempAnswerDir);
-        for (File f : t1.listFiles()) {
-            FileUtils.deleteQuietly(f);
-        }
-        File t2 = new File(tempAnswerZipDir);
-        for (File f : t2.listFiles()) {
-            FileUtils.deleteQuietly(f);
-        }*/
+        final String tempAnswerDir = systemConfig.getTempDataDir() + "/tempAnswer";
 
         Long startId = 0L;
-
-        /*String json1 = "{\n" +
-                "    \"cost\": 6317,\n" +
-                "    \"nextExamRecordDataId\": 80703,\n" +
-                "    \"subjectiveAnswerList\": [{\n" +
-                "        \"examId\": 198,\n" +
-                "        \"courseCode\": \"6000459\",\n" +
-                "        \"examStudentId\": 1837550,\n" +
-                "        \"examRecordDataId\": 80507,\n" +
-                "        \"mainNumber\": 5,\n" +
-                "        \"questionId\": \"5c45384def8fce45257834d8\",\n" +
-                "        \"order\": 19,\n" +
-                "        \"studentAnswer\": \"https://ecs-test-static.qmth.com.cn/oe-answer-file/1837550/80507/19/1837550_80507_19_1558433562519.jpg|https://ecs-test-static.qmth.com.cn/oe-answer-file/1837550/80507/19/1837550_80507_19_1558433889599.png\",\n" +
-                "        \"answerType\": \"SINGLE_AUDIO\",\n" +
-                "        \"realAnswerType\": \"audio\"\n" +
-                "    }, {\n" +
-                "        \"examId\": 198,\n" +
-                "        \"courseCode\": \"6000459\",\n" +
-                "        \"examStudentId\": 1837550,\n" +
-                "        \"examRecordDataId\": 80507,\n" +
-                "        \"mainNumber\": 5,\n" +
-                "        \"questionId\": \"5c45384def8fce45257834d9\",\n" +
-                "        \"order\": 20,\n" +
-                "        \"studentAnswer\": null,\n" +
-                "        \"answerType\": \"SINGLE_AUDIO\",\n" +
-                "        \"realAnswerType\": \"audio\"\n" +
-                "    }, {\n" +
-                "        \"examId\": 198,\n" +
-                "        \"courseCode\": \"6000459\",\n" +
-                "        \"examStudentId\": 1837550,\n" +
-                "        \"examRecordDataId\": 80507,\n" +
-                "        \"mainNumber\": 6,\n" +
-                "        \"questionId\": \"5c45384def8fce45257834da\",\n" +
-                "        \"order\": 23,\n" +
-                "        \"studentAnswer\": null,\n" +
-                "        \"answerType\": null,\n" +
-                "        \"realAnswerType\": \"text\"\n" +
-                "    }]\n" +
-                "}";*/
-
         while (true) {
             GetSubjectiveAnswerReq req = new GetSubjectiveAnswerReq();
             req.setExamId(examId);
             req.setStartExamRecordId(startId);
 
-
             GetSubjectiveAnswerResp resp = examRecordQuestionsCloudService.getSubjectiveAnswerList(req);
-
-            /*GetSubjectiveAnswerResp resp = JsonUtil.fromJson(json1, GetSubjectiveAnswerResp.class);*/
+//            GetSubjectiveAnswerResp resp = JsonUtil.fromJson(testJson(), GetSubjectiveAnswerResp.class);
             if (resp.getNextExamRecordDataId().equals(startId)) {
                 break;
             }
 
-
             List<SubjectiveAnswerBean> subjectiveAnswerList = resp.getSubjectiveAnswerList();
 
             List<Long> examStudentIdList = subjectiveAnswerList.stream().
@@ -158,7 +108,6 @@ public class CloudMarkingController {
                     answerMap.put("body", bodyMap);
 
                     answerMapList.add(answerMap);
-
                 }
 
                 //将考生结果保存至临时文件
@@ -166,11 +115,10 @@ public class CloudMarkingController {
                 String filePath = tempAnswerDir + "/" + examId + "-" + courseCode + "-" + examStudentId + ".json";
                 IOUtil.toFile(json.getBytes(Charset.forName("UTF-8")), filePath);
             }
-
-
             startId = resp.getNextExamRecordDataId();
         }
-        String zipPath = tempAnswerZipDir + "/" + examId + "-" + System.currentTimeMillis() + ".zip";
+
+        String zipPath = tempAnswerDir + "/" + examId + "-" + System.currentTimeMillis() + ".zip";
         File zipFile = new File(zipPath);
         ZipUtil.zip(new File(tempAnswerDir), zipFile);
 
@@ -181,6 +129,60 @@ public class CloudMarkingController {
                 header("Content-Disposition", "attachment; filename=" + downLoadFileName).
                 contentType(MediaType.APPLICATION_OCTET_STREAM).body(bytes);
 
+        clearTempAnswerFile(tempAnswerDir);
         return responseEntity;
     }
+
+    /**
+     * 清空临时目录中的文件
+     *
+     * @param tempAnswerDir
+     */
+    private void clearTempAnswerFile(String tempAnswerDir) {
+        File t1 = new File(tempAnswerDir);
+        for (File f : t1.listFiles()) {
+            FileUtils.deleteQuietly(f);
+        }
+    }
+
+    private String testJson() {
+        return "{\n" +
+                "    \"cost\": 6317,\n" +
+                "    \"nextExamRecordDataId\": 80703,\n" +
+                "    \"subjectiveAnswerList\": [{\n" +
+                "        \"examId\": 198,\n" +
+                "        \"courseCode\": \"6000459\",\n" +
+                "        \"examStudentId\": 1837550,\n" +
+                "        \"examRecordDataId\": 80507,\n" +
+                "        \"mainNumber\": 5,\n" +
+                "        \"questionId\": \"5c45384def8fce45257834d8\",\n" +
+                "        \"order\": 19,\n" +
+                "        \"studentAnswer\": \"https://ecs-test-static.qmth.com.cn/oe-answer-file/1837550/80507/19/1837550_80507_19_1558433562519.jpg|https://ecs-test-static.qmth.com.cn/oe-answer-file/1837550/80507/19/1837550_80507_19_1558433889599.png\",\n" +
+                "        \"answerType\": \"SINGLE_AUDIO\",\n" +
+                "        \"realAnswerType\": \"audio\"\n" +
+                "    }, {\n" +
+                "        \"examId\": 198,\n" +
+                "        \"courseCode\": \"6000459\",\n" +
+                "        \"examStudentId\": 1837550,\n" +
+                "        \"examRecordDataId\": 80507,\n" +
+                "        \"mainNumber\": 5,\n" +
+                "        \"questionId\": \"5c45384def8fce45257834d9\",\n" +
+                "        \"order\": 20,\n" +
+                "        \"studentAnswer\": null,\n" +
+                "        \"answerType\": \"SINGLE_AUDIO\",\n" +
+                "        \"realAnswerType\": \"audio\"\n" +
+                "    }, {\n" +
+                "        \"examId\": 198,\n" +
+                "        \"courseCode\": \"6000459\",\n" +
+                "        \"examStudentId\": 1837550,\n" +
+                "        \"examRecordDataId\": 80507,\n" +
+                "        \"mainNumber\": 6,\n" +
+                "        \"questionId\": \"5c45384def8fce45257834da\",\n" +
+                "        \"order\": 23,\n" +
+                "        \"studentAnswer\": null,\n" +
+                "        \"answerType\": null,\n" +
+                "        \"realAnswerType\": \"text\"\n" +
+                "    }]\n" +
+                "}";
+    }
 }