소스 검색

新增wps上传文件和分数查询

wangliang 1 년 전
부모
커밋
c146f09de7

+ 4 - 1
server/src/main/java/com/qmth/jkserver/constant/SystemConstant.java

@@ -90,9 +90,12 @@ public class SystemConstant {
     public static final String WPS_DOMAIN = "wps.domain";
     public static final String WPS_ACCESS_KEY = "wps.access.key";
     public static final String WPS_SECRET_KEY = "wps.secret.key";
-    public static final String CONTENT_TYPE = "application/json";
+    public static final String CONTENT_TYPE_JSON = "application/json";
+    public static final String CONTENT_TYPE_FORM_DATA = "multipart/form-data";
     public static final String QUESTION_LIST = "/api/question/list";
     public static final String QUESTION_DETAIL = "/api/question/detail";
+    public static final String QUESTION_CALCULATE_QUERY = "/api/question/calculate/query";
+    public static final String QUESTION_CALCULATE_APPLY = "/api/question/calculate/upload/file/apply";
     public static final String WPS = "wps";
     public static final String WPS_DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
     public static final String WPS_QUESTION = "wps_question";

+ 1 - 1
server/src/main/java/com/qmth/jkserver/controller/api/OpenController.java

@@ -142,7 +142,7 @@ public class OpenController extends BaseSystemController {
         }
         response.setStatus(HttpStatus.HTTP_OK);
         response.setCharacterEncoding(SystemConstant.CHARSET_NAME);
-        response.setContentType(SystemConstant.CONTENT_TYPE + ";charset=" + SystemConstant.CHARSET_NAME);
+        response.setContentType(SystemConstant.CONTENT_TYPE_JSON + ";charset=" + SystemConstant.CHARSET_NAME);
         JSONObject jsonObject = new JSONObject();
         jsonObject.put("success", true);
         response.getWriter().print(jsonObject.toJSONString());

+ 18 - 0
server/src/main/java/com/qmth/jkserver/controller/api/WpsController.java

@@ -107,6 +107,24 @@ public class WpsController extends BaseSystemController {
         return ResultUtil.ok(result);
     }
 
+    @ApiOperation(value = "题库算分接口")
+    @RequestMapping(value = "/question/calculate/apply", method = RequestMethod.POST)
+    @ApiResponses({@ApiResponse(code = 200, message = "题库算分接口信息", response = Object.class)})
+    @TokenValidate(policy = TokenPolicy.IGNORE)
+    public Object questionCalculateApply(@ApiParam(value = "题库id", required = true) @RequestParam String questionId,
+                                         @ApiParam(value = "题库id", required = true) @RequestParam String requestId,
+                                         @ApiParam(value = "上传文件", required = true) @RequestParam List<MultipartFile> files) throws IOException, NoSuchAlgorithmException {
+        return ResultUtil.ok(WpsUtil.exection(new JSONObject(ImmutableMap.of("questionId", questionId, "requestId", requestId)).toJSONString(), ImmutableMap.of("questionId", questionId, "requestId", requestId, "files", files.get(0)), SystemConstant.QUESTION_CALCULATE_APPLY));
+    }
+
+    @ApiOperation(value = "题库查分接口")
+    @RequestMapping(value = "/question/calculate/query", method = RequestMethod.POST)
+    @ApiResponses({@ApiResponse(code = 200, message = "题库查分接口信息", response = Object.class)})
+    @TokenValidate(policy = TokenPolicy.IGNORE)
+    public Object questionCalculateQuery(@ApiParam(value = "算分申请code", required = true) @RequestParam String applyCode) throws IOException, NoSuchAlgorithmException {
+        return ResultUtil.ok(WpsUtil.exection(new JSONObject(Collections.singletonMap("applyCode", applyCode)).toJSONString(), SystemConstant.QUESTION_CALCULATE_QUERY));
+    }
+
     @ApiOperation(value = "清除缓存接口")
     @RequestMapping(value = "/delete/cache", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "清除缓存接口信息", response = Object.class)})

+ 1 - 1
server/src/main/java/com/qmth/jkserver/core/inteceptor/ApiInterceptor.java

@@ -104,7 +104,7 @@ public class ApiInterceptor extends HandlerInterceptorAdapter {
             logger.debug("响应信息:" + returnResult);
             response.setStatus(200);
             response.setCharacterEncoding(SystemConstant.CHARSET_NAME);
-            response.setContentType(SystemConstant.CONTENT_TYPE + ";charset=" + SystemConstant.CHARSET_NAME);
+            response.setContentType(SystemConstant.CONTENT_TYPE_JSON + ";charset=" + SystemConstant.CHARSET_NAME);
             response.getWriter().write(returnResult);
         } catch (Exception e) {
         }

+ 1 - 1
server/src/main/java/com/qmth/jkserver/core/inteceptor/SystemInterceptor.java

@@ -95,7 +95,7 @@ public class SystemInterceptor extends HandlerInterceptorAdapter {
             ObjectMapper mapper = new ObjectMapper();
             response.setStatus(200);
             response.setCharacterEncoding(SystemConstant.CHARSET_NAME);
-            response.setContentType(SystemConstant.CONTENT_TYPE + ";charset=" + SystemConstant.CHARSET_NAME);
+            response.setContentType(SystemConstant.CONTENT_TYPE_JSON + ";charset=" + SystemConstant.CHARSET_NAME);
             response.getWriter().write(mapper.writeValueAsString(result));
         } catch (Exception e) {
         }

+ 60 - 5
server/src/main/java/com/qmth/jkserver/util/HttpUtil.java

@@ -3,6 +3,7 @@ package com.qmth.jkserver.util;
 import com.qmth.jkserver.constant.SystemConstant;
 import com.qmth.jkserver.core.exception.JkServerException;
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
 import org.apache.http.Consts;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
@@ -22,10 +23,11 @@ import org.apache.http.message.BasicHeader;
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.protocol.HTTP;
 import org.apache.http.util.EntityUtils;
-import org.apache.tomcat.util.http.fileupload.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.io.*;
 import java.net.URLEncoder;
@@ -260,13 +262,13 @@ public class HttpUtil {
         post.setConfig(requestConfig);
         post.setHeader(SystemConstant.HEADER_AUTHORIZATION, secret);
         post.setHeader(SystemConstant.HEADER_TIME, String.valueOf(timestamp));
-        post.setHeader(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE + "; charset=" + SystemConstant.CHARSET_NAME);
-        post.setHeader("Accept", SystemConstant.CONTENT_TYPE);
+        post.setHeader(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE_JSON + "; charset=" + SystemConstant.CHARSET_NAME);
+        post.setHeader("Accept", SystemConstant.CONTENT_TYPE_JSON);
 
         String encoderJson = URLEncoder.encode(json, SystemConstant.CHARSET_NAME);
         StringEntity se = new StringEntity(encoderJson);
         se.setContentType("text/json");
-        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE));
+        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE_JSON));
         post.setEntity(se);
         // 执行请求,获取响应
         return getRespString(post);
@@ -297,9 +299,62 @@ public class HttpUtil {
 //        StringEntity se = new StringEntity(encoderJson);
         StringEntity se = new StringEntity(json);
         se.setContentType("text/json");
-        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE));
+        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE_JSON));
         post.setEntity(se);
         // 执行请求,获取响应
         return getRespString(post);
     }
+
+    /**
+     * post file
+     *
+     * @param url
+     * @param params
+     * @param headers
+     * @return
+     * @throws IOException
+     */
+    public static String postFile(String url, Map<String, Object> params, Map<String, String> headers) throws IOException {
+        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(SystemConstant.CONNECT_TIME_OUT)// 连接主机服务超时时间
+                .setConnectionRequestTimeout(SystemConstant.CONNECT_TIME_OUT)// 请求超时时间
+                .setSocketTimeout(SystemConstant.SOCKET_CONNECT_TIME_OUT)// 数据读取超时时间
+                .build();
+        HttpPost post = new HttpPost(url);
+        for (Map.Entry<String, String> entry : headers.entrySet()) {
+            post.setHeader(entry.getKey(), entry.getValue());
+        }
+
+        File file = null;
+        try {
+            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
+            // 解决中文文件名乱码问题
+            entityBuilder.setMode(HttpMultipartMode.RFC6532);
+            entityBuilder.setCharset(Consts.UTF_8);
+            ContentType contentType = ContentType.create(ContentType.MULTIPART_FORM_DATA.getMimeType(), Consts.UTF_8);
+            if (!CollectionUtils.isEmpty(params)) {
+                for (String key : params.keySet()) {
+                    if (key.contains("files")) {
+                        MultipartFile multipartFile = (MultipartFile) params.get(key);
+                        String[] strs = StringUtils.split(multipartFile.getOriginalFilename(), ".");
+                        file = SystemConstant.getFileTempVar("." + strs[1]);
+                        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
+                        entityBuilder.addBinaryBody(key, file, ContentType.DEFAULT_BINARY, file.getName());
+                    } else {
+                        entityBuilder.addPart(key, new StringBody(String.valueOf(params.get(key)), contentType));
+                    }
+                }
+            }
+            entityBuilder.setContentType(ContentType.MULTIPART_FORM_DATA);
+            post.setEntity(entityBuilder.build());
+            post.setConfig(requestConfig);
+            return getRespString(post);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (Objects.nonNull(file)) {
+                file.delete();
+            }
+        }
+        return null;
+    }
 }

+ 43 - 2
server/src/main/java/com/qmth/jkserver/util/WpsUtil.java

@@ -84,10 +84,10 @@ public class WpsUtil {
         String contentMd5 = MD5Util.encoder(content);
         headers.put("Content-Md5", contentMd5);
         String date = getGMTDateString();
-        String signature = String.format(Locale.US, "WPS-3:%s:%s", sysConfigWpsAccessKey.getConfigValue(), sha1(sysConfigWpsSecretKey.getConfigValue() + contentMd5 + api + SystemConstant.CONTENT_TYPE + date));
+        String signature = String.format(Locale.US, "WPS-3:%s:%s", sysConfigWpsAccessKey.getConfigValue(), sha1(sysConfigWpsSecretKey.getConfigValue() + contentMd5 + api + SystemConstant.CONTENT_TYPE_JSON + date));
         headers.put("X-Auth", signature);
         headers.put("DATE", date);
-        headers.put(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE);
+        headers.put(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE_JSON);
 
         String result = HttpUtil.postJson(sysConfigWpsDomain.getConfigValue() + api, content, headers);
         if (Objects.nonNull(result) && !Objects.equals(result.trim(), "")) {
@@ -95,4 +95,45 @@ public class WpsUtil {
         }
         return result;
     }
+
+    /**
+     * 执行数据方法
+     *
+     * @param content
+     * @param params
+     * @param api
+     * @return
+     * @throws NoSuchAlgorithmException
+     */
+    public static String exection(String content, Map<String, Object> params, String api) throws NoSuchAlgorithmException, IOException {
+        Objects.requireNonNull(content, "内容不能为空");
+        CommonCacheService commonCacheService = SpringContextHolder.getBean(CommonCacheService.class);
+
+        SysConfig sysConfigWpsDomain = commonCacheService.addSysConfigCache(SystemConstant.WPS_DOMAIN);
+        Objects.requireNonNull(sysConfigWpsDomain, "wps域名不能为空");
+        Objects.requireNonNull(sysConfigWpsDomain.getConfigValue(), "wps域名值不能为空");
+
+        SysConfig sysConfigWpsAccessKey = commonCacheService.addSysConfigCache(SystemConstant.WPS_ACCESS_KEY);
+        Objects.requireNonNull(sysConfigWpsAccessKey, "wps access key不能为空");
+        Objects.requireNonNull(sysConfigWpsAccessKey.getConfigValue(), "wps access key值不能为空");
+
+        SysConfig sysConfigWpsSecretKey = commonCacheService.addSysConfigCache(SystemConstant.WPS_SECRET_KEY);
+        Objects.requireNonNull(sysConfigWpsSecretKey, "wps secret key不能为空");
+        Objects.requireNonNull(sysConfigWpsSecretKey.getConfigValue(), "wps secret key值不能为空");
+
+        Map<String, String> headers = new HashMap<>();
+        String contentMd5 = MD5Util.encoder(content);
+        headers.put("Content-Md5", contentMd5);
+        String date = getGMTDateString();
+        String signature = String.format(Locale.US, "WPS-3:%s:%s", sysConfigWpsAccessKey.getConfigValue(), sha1(sysConfigWpsSecretKey.getConfigValue() + contentMd5 + api + SystemConstant.CONTENT_TYPE_FORM_DATA + date));
+        headers.put("X-Auth", signature);
+        headers.put("DATE", date);
+//        headers.put(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE_FORM_DATA);
+
+        String result = HttpUtil.postFile(sysConfigWpsDomain.getConfigValue() + api, params, headers);
+        if (Objects.nonNull(result) && !Objects.equals(result.trim(), "")) {
+            log.info("result:{}", JacksonUtil.parseJson(result));
+        }
+        return result;
+    }
 }