|
@@ -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 {
|