Преглед изворни кода

导出考试答案代码调整和优化

lideyin пре 5 година
родитељ
комит
c04e5ce72a

+ 98 - 18
src/main/java/cn/com/qmth/examcloud/bridge/modules/cloudmarking/controller/CloudMarkingClientController.java

@@ -4,50 +4,41 @@ import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.commons.util.HttpClientUtil;
 import cn.com.qmth.examcloud.commons.util.IOUtil;
 import cn.com.qmth.examcloud.commons.util.JsonUtil;
-import cn.com.qmth.examcloud.commons.util.ZipUtil;
+import cn.com.qmth.examcloud.commons.util.RegExpUtil;
 import cn.com.qmth.examcloud.core.oe.admin.api.ExamRecordCloudService;
 import cn.com.qmth.examcloud.core.oe.admin.api.ExamRecordQuestionsCloudService;
 import cn.com.qmth.examcloud.core.oe.admin.api.OeExamStudentCloudService;
-import cn.com.qmth.examcloud.core.oe.admin.api.bean.SubjectiveAnswerBean;
 import cn.com.qmth.examcloud.core.oe.admin.api.bean.ToBeMarkExamRecordBean;
 import cn.com.qmth.examcloud.core.oe.admin.api.bean.ToBeMarkExamStudentBean;
 import cn.com.qmth.examcloud.core.oe.admin.api.bean.ToBeMarkSubjectiveAnswerBean;
-import cn.com.qmth.examcloud.core.oe.admin.api.request.GetSubjectiveAnswerReq;
-import cn.com.qmth.examcloud.core.oe.admin.api.request.GetToBeMarkExamRecordReq;
 import cn.com.qmth.examcloud.core.oe.admin.api.request.GetToBeMarkExamStudentReq;
-import cn.com.qmth.examcloud.core.oe.admin.api.response.GetSubjectiveAnswerResp;
 import cn.com.qmth.examcloud.core.oe.admin.api.response.GetToBeMarkExamRecordResp;
 import cn.com.qmth.examcloud.core.oe.admin.api.response.GetToBeMarkExamStudentResp;
 import cn.com.qmth.examcloud.web.config.SystemProperties;
 import com.google.common.collect.Maps;
 import com.mysql.cj.util.StringUtils;
-import org.apache.commons.io.FileUtils;
-import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
-import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.util.EntityUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.io.Closeable;
-import java.io.File;
 import java.io.IOException;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.stream.Collectors;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * @Description 云阅卷客户端接口
@@ -141,7 +132,7 @@ public class CloudMarkingClientController {
 
                 //答案临时存放目录
                 final String tempAnswerDir =
-                        systemConfig.getTempDataDir() + "/exam-" + rb.getExamId() + "/tempAnswer/" + rb.getCourseCode() + "-" + rb.getPaperType();
+                        systemConfig.getTempDataDir() + "/exam-" + rb.getExamId() + "/" + rb.getCourseCode() + "-" + rb.getPaperType();
 
                 List<Map<String, Object>> answerMapList = new ArrayList<>();
 
@@ -157,10 +148,37 @@ public class CloudMarkingClientController {
                     Map<String, Object> sectionMap = Maps.newHashMap();
                     List<Map<String, Object>> blockMapList = new ArrayList<>();
 
-                    Map<String, Object> blockMap = Maps.newHashMap();
-                    blockMap.put("type", sab.getRealAnswerType());
-                    blockMap.put("value", sab.getStudentAnswer());
-                    blockMapList.add(blockMap);
+                    //校验答案格式,校验成功才追加至集合,否则不追加
+                    if (validateAnswer(sab.getRealAnswerType(), sab.getStudentAnswer())) {
+                        //图片文件特殊处理
+                        if (!StringUtils.isNullOrEmpty(sab.getStudentAnswer()) &&
+                                "image".equals(sab.getRealAnswerType())) {
+                            if (sab.getStudentAnswer().indexOf("|") > -1) {
+                                String[] imgAnswers = sab.getStudentAnswer().split("\\|");
+                                for (int i = 0; i < imgAnswers.length; i++) {
+                                    Map<String, Object> blockMap = Maps.newHashMap();
+                                    blockMap.put("type", sab.getRealAnswerType());
+                                    blockMap.put("value", transformedAnswer(sab.getRealAnswerType(), imgAnswers[i]));
+
+                                    //又拍云图片的宽*高
+                                    String imgWH = RegExpUtil.find(imgAnswers[i], "(\\d+)x(\\d+)");
+                                    if (!StringUtils.isNullOrEmpty(imgWH)) {
+                                        Map<String, Object> paramMap = new HashMap<>();
+                                        paramMap.put("width", Integer.valueOf(imgWH.split("x")[0]));
+                                        paramMap.put("height", Integer.valueOf(imgWH.split("x")[1]));
+                                        blockMap.put("param", paramMap);
+                                    }
+
+                                    blockMapList.add(blockMap);
+                                }
+                            }
+                        } else {
+                            Map<String, Object> blockMap = Maps.newHashMap();
+                            blockMap.put("type", sab.getRealAnswerType());
+                            blockMap.put("value", transformedAnswer(sab.getRealAnswerType(), sab.getStudentAnswer()));
+                            blockMapList.add(blockMap);
+                        }
+                    }
 
                     sectionMap.put("blocks", blockMapList);
                     sectionMapList.add(sectionMap);
@@ -186,4 +204,66 @@ public class CloudMarkingClientController {
         }
     }
 
+    private String transformedAnswer(String answerType, String studentAnswer) {
+        if ("image".equals(answerType)) {
+            String regExp = "(ftp|https?)\\:\\/\\/([\\w\\_\\-]+)\\.([\\w\\-]+[\\.]?)*[\\w]+\\.[a-zA-Z]{2,10}(.*)\\.(png|jpg|gif|jpeg)";
+            return RegExpUtil.find(studentAnswer, regExp);
+        }
+        return studentAnswer;
+    }
+
+    /**
+     * 校验答案格式是否正确
+     *
+     * @param realAnswerType
+     * @param studentAnswer
+     * @return
+     */
+    private boolean validateAnswer(String realAnswerType, String studentAnswer) {
+        if (StringUtils.isNullOrEmpty(studentAnswer)) {
+            return false;
+        }
+
+        String regExp;
+        switch (realAnswerType) {
+            case "image":
+                //图片必须包含.png|jpg|gif|jpeg
+                regExp = "^(ftp|https?)\\:\\/\\/([\\w\\_\\-]+)\\.([\\w\\-]+[\\.]?)*[\\w]+\\.[a-zA-Z]{2,10}(.*)\\.(png|jpg|gif|jpeg).*$";
+                return studentAnswer.matches(regExp);
+            case "audio":
+                //音频必须包含.mp3
+                regExp = "^(ftp|https?)\\:\\/\\/([\\w\\_\\-]+)\\.([\\w\\-]+[\\.]?)*[\\w]+\\.[a-zA-Z]{2,10}(.*)\\.(mp3)";
+                return studentAnswer.matches(regExp);
+        }
+
+        return true;
+    }
+
+    /**
+     * 校验字符串和正则表达式是否匹配
+     *
+     * @param regExp
+     * @param input
+     * @return
+     */
+    private boolean isRegExpMatch(String regExp, String input) {
+        Pattern pattern = Pattern.compile(regExp);
+        Matcher matcher = pattern.matcher(input);
+        return matcher.matches();
+    }
+
+    public static void main(String[] args) {
+        String regExp = "^(ftp|https?)\\:\\/\\/([\\w\\_\\-]+)\\.([\\w\\-]+[\\.]?)*[\\w]+\\.[a-zA-Z]{2,10}(.*)\\.(png|jpg|gif|jpeg)(\\s|\\S)*$";
+        String regExp1 = "(ftp|https?)\\:\\/\\/([\\w\\_\\-]+)\\.([\\w\\-]+[\\.]?)*[\\w]+\\.[a-zA-Z]{2,10}(.*)\\.(png|jpg|gif|jpeg)";
+        String regExp2 = "(\\d+)x(\\d+)";
+        String input = "https://ecs-test-static.qmth.com.cn/oe-answer-file/3/7/20/3_7_20_15748449608356000.jpeg!/both/200x200";
+        Pattern pattern = Pattern.compile(regExp);
+        Matcher matcher = pattern.matcher(input);
+//
+        System.out.println(matcher.matches());
+        System.out.println(matcher.group());
+        System.out.println(RegExpUtil.find(input, regExp1));
+        System.out.println(RegExpUtil.find(input, regExp2));
+
+    }
 }