deason пре 7 година
родитељ
комит
4374c06ddd

+ 12 - 6
src/main/java/cn/com/qmth/examcloud/app/controller/v1/OfflineExamRestController.java

@@ -20,7 +20,6 @@ import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.MultipartHttpServletRequest;
 
 import javax.servlet.http.HttpServletRequest;
-import java.io.File;
 
 /**
  * 离线考试相关接口
@@ -51,11 +50,18 @@ public class OfflineExamRestController {
 
     @ApiOperation(value = "上传作答文件接口")
     @RequestMapping(value = "/paper/answer/upload", method = {RequestMethod.GET, RequestMethod.POST})
-    public Result uploadPaperAnswer(@RequestHeader String key, @RequestHeader String token, @RequestParam String examRecordId, @RequestParam String fileType,
-                                    @RequestParam(required = false) String fileMd5, HttpServletRequest request) throws Exception {
-        MultipartFile multipart = ((MultipartHttpServletRequest) request).getFile("file");
-        File file = null;//todo
-        return netExamService.uploadPaperAnswer(key, token, examRecordId, file, fileType, fileMd5);
+    public Result uploadPaperAnswer(@RequestHeader String key, @RequestHeader String token, @RequestParam String examRecordId,
+                                    @RequestParam(required = false) String md5, HttpServletRequest request) throws Exception {
+        MultipartFile multipart = null;
+        try {
+            multipart = ((MultipartHttpServletRequest) request).getFile("file");
+        } catch (Exception e) {
+            log.error(e.getMessage());
+        }
+        if (multipart == null) {
+            return new Result().error("请选择要上传文件!");
+        }
+        return netExamService.uploadPaperAnswer(key, token, examRecordId, multipart.getBytes(), multipart.getOriginalFilename(), md5);
     }
 
     @ApiOperation(value = "获取某份试卷的详细信息接口")

+ 4 - 0
src/main/java/cn/com/qmth/examcloud/app/model/Constants.java

@@ -15,6 +15,10 @@ public interface Constants {
 
     String PARAM_TOKEN = "token";
 
+    String TYPE_ZIP = "zip";
+
+    String TYPE_PDF = "pdf";
+
     String DEFAULT_ACCOUNT_TYPE = "COMMON_LOGIN_NAME";
 
     /* 常用状态码 */

+ 31 - 6
src/main/java/cn/com/qmth/examcloud/app/service/NetExamService.java

@@ -7,19 +7,22 @@
 
 package cn.com.qmth.examcloud.app.service;
 
+import cn.com.qmth.examcloud.app.exception.ApiException;
 import cn.com.qmth.examcloud.app.model.Result;
 import cn.com.qmth.examcloud.app.utils.DateUtils;
 import cn.com.qmth.examcloud.app.utils.HttpUtils;
+import okhttp3.MediaType;
+import okhttp3.MultipartBody;
 import okhttp3.Request;
+import okhttp3.RequestBody;
+import org.apache.commons.io.FilenameUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.util.Assert;
 
-import java.io.File;
-
-import static cn.com.qmth.examcloud.app.model.Constants.PARAM_KEY;
-import static cn.com.qmth.examcloud.app.model.Constants.PARAM_TOKEN;
+import static cn.com.qmth.examcloud.app.model.Constants.*;
 
 /**
  * 网考业务服务接口
@@ -74,8 +77,30 @@ public class NetExamService {
         return HttpUtils.call(request);
     }
 
-    public Result uploadPaperAnswer(String key, String token, String examRecordId, File file, String fileType, String fileMd5) throws Exception {
-        return null;
+    public Result uploadPaperAnswer(String key, String token, String examRecordId, byte[] fileBytes, String fileName, String md5) throws Exception {
+        Assert.notNull(examRecordId, "FileName must not be null.");
+        Assert.notNull(fileName, "ExamRecordId must not be null.");
+        String fileType = FilenameUtils.getExtension(fileName.toLowerCase());
+        if (!TYPE_ZIP.equals(fileType) && !TYPE_PDF.equals(fileType)) {
+            throw new ApiException("FileType must be zip or pdf.");
+        }
+        if (fileBytes.length == 0) {
+            throw new ApiException("File must be not empty.");
+        }
+        final String requestUrl = String.format(propertyService.getNetExamUrl() + "/api/offline_exam/%s/submit", examRecordId);
+        MultipartBody.Builder formBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
+        RequestBody body = RequestBody.create(MediaType.parse("application/octet-stream"), fileBytes);
+        formBody.addFormDataPart("file", fileName, body);
+        formBody.addFormDataPart("fileType", fileType);
+        formBody.addFormDataPart("md5", md5);
+        Request request = new Request.Builder()
+                .url(requestUrl)
+                .post(formBody.build())
+                .addHeader(PARAM_KEY, key)
+                .addHeader(PARAM_TOKEN, token)
+                .build();
+        //执行请求
+        return HttpUtils.call(request);
     }
 
     public Result getPracticeExamCourseList(String key, String token, String examId) throws Exception {

+ 36 - 0
src/main/resources/static/upload.html

@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<html lang="zh-cn">
+<head>
+    <meta name="renderer" content="webkit"/>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
+    <script src="bycdao-ui/jquery/jquery-1.9.1.min.js"></script>
+</head>
+<body>
+<form id="uploadForm" enctype="multipart/form-data">
+    <input type="file" name="file"/>
+    <input type="button" value="提交" onclick="doUpload()"/>
+</form>
+<script>
+    function doUpload() {
+        var formData = new FormData($('#uploadForm')[0]);
+        formData.append('examRecordId', '101262');
+        formData.append('md5', '');
+        $.ajax({
+            url: 'http://localhost:8081/api/v1/exam/offline/paper/answer/upload',
+            type: "POST",
+            data: formData,
+            headers: {
+                'key': 'U_S_109_53308',
+                'token': '9e3eb5be1d2a4a9e9ffab4341af7b987'
+            },
+            processData: false,
+            contentType: false,
+            success: function (data) {
+                console.log(data);
+            }
+        });
+    }
+</script>
+</body>
+</html>