WANG 6 tahun lalu
induk
melakukan
be40b267fa

+ 37 - 14
src/main/java/cn/com/qmth/examcloud/web/cloud/CloudClientSupport.java

@@ -1,6 +1,6 @@
 package cn.com.qmth.examcloud.web.cloud;
 
-import java.io.File;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
@@ -15,6 +15,7 @@ import org.springframework.util.MultiValueMap;
 import org.springframework.web.client.RestTemplate;
 
 import cn.com.qmth.examcloud.api.commons.exchange.BaseRequest;
+import cn.com.qmth.examcloud.api.commons.exchange.FormFileDataPart;
 import cn.com.qmth.examcloud.api.commons.exchange.FormRequest;
 import cn.com.qmth.examcloud.api.commons.exchange.JsonSerializable;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
@@ -149,20 +150,39 @@ public abstract class CloudClientSupport {
 	 * @return
 	 */
 	protected <T> T exchange(String url, HttpMethod method, Object body, Class<T> responseType) {
+		return exchange(url, method, null, body, responseType);
+	}
+
+	/**
+	 * exchange
+	 *
+	 * @author WANGWEI
+	 * @param url
+	 * @param method
+	 * @param httpHeaders
+	 * @param body
+	 * @param responseType
+	 * @return
+	 */
+	protected <T> T exchange(String url, HttpMethod method, HttpHeaders httpHeaders, Object body,
+			Class<T> responseType) {
 
 		long startTime = System.currentTimeMillis();
 
-		HttpHeaders requestHeaders = new HttpHeaders();
-		requestHeaders.add("Trace-Id", ThreadLocalUtil.getTraceId());
-		requestHeaders.add("timestamp", String.valueOf(startTime));
-		requestHeaders.add("App-Id", String.valueOf(AppSelfHolder.get().getAppId()));
-		requestHeaders.add("App-Code", String.valueOf(AppSelfHolder.get().getAppCode()));
+		if (null == httpHeaders) {
+			httpHeaders = new HttpHeaders();
+		}
+
+		httpHeaders.add("Trace-Id", ThreadLocalUtil.getTraceId());
+		httpHeaders.add("timestamp", String.valueOf(startTime));
+		httpHeaders.add("App-Id", String.valueOf(AppSelfHolder.get().getAppId()));
+		httpHeaders.add("App-Code", String.valueOf(AppSelfHolder.get().getAppCode()));
 
 		String joinStr = StringUtil.join(AppSelfHolder.get().getAppId(),
 				AppSelfHolder.get().getAppCode(), startTime, AppSelfHolder.get().getSecretKey());
 		byte[] bytes = SHA256.encode(joinStr);
 		String accessToken = ByteUtil.toHexAscii(bytes);
-		requestHeaders.add("Access-Token", accessToken);
+		httpHeaders.add("Access-Token", accessToken);
 
 		if (INTERFACE_LOG.isInfoEnabled()) {
 			INTERFACE_LOG.info("[CALL-IN]. url=" + url);
@@ -175,9 +195,9 @@ public abstract class CloudClientSupport {
 
 		HttpEntity<Object> requestEntity = null;
 		if (null == body) {
-			requestEntity = new HttpEntity<Object>(requestHeaders);
+			requestEntity = new HttpEntity<Object>(httpHeaders);
 		} else {
-			requestEntity = new HttpEntity<Object>(body, requestHeaders);
+			requestEntity = new HttpEntity<Object>(body, httpHeaders);
 		}
 
 		T respBody = null;
@@ -264,19 +284,22 @@ public abstract class CloudClientSupport {
 		String url = buildUrl(appName, requestMapping);
 
 		MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
-		FileSystemResource resource = new FileSystemResource(new File(req.getFilePath()));
-		params.add("file", resource);
+		HttpHeaders httpHeaders = new HttpHeaders();
+
+		List<FormFileDataPart> fileDataPartList = req.getFileDataPartList();
+		for (FormFileDataPart fileDataPart : fileDataPartList) {
+			FileSystemResource resource = new FileSystemResource(fileDataPart.getFile());
+			params.add(fileDataPart.getParamName(), resource);
+		}
 
 		String json = JsonUtil.toJson(req);
 		Map<String, String> otherParams = JsonUtil.json2Map(json);
-		otherParams.remove("file");
-		otherParams.remove("filePath");
 
 		for (Entry<String, String> entry : otherParams.entrySet()) {
 			params.add(entry.getKey(), entry.getValue());
 		}
 
-		return exchange(url, HttpMethod.POST, params, responseType);
+		return exchange(url, HttpMethod.POST, httpHeaders, params, responseType);
 	}
 
 }