|
@@ -5,21 +5,26 @@ import cn.kdan.pdf.backend.core.pojo.comPdfKit.ComPdfKitOauthData;
|
|
|
import cn.kdan.pdf.backend.core.pojo.comPdfKit.ComPdfKitResponse;
|
|
|
import cn.kdan.pdf.backend.core.pojo.comPdfKit.CreateTaskResult;
|
|
|
import cn.kdan.pdf.backend.core.pojo.comPdfKit.UploadFileResult;
|
|
|
+import constant.CommonConstant;
|
|
|
import exception.BackendRuntimeException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
|
|
import org.springframework.core.ParameterizedTypeReference;
|
|
|
+import org.springframework.core.io.FileSystemResource;
|
|
|
import org.springframework.http.*;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
import utils.JsonUtils;
|
|
|
import utils.RedisUtils;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
import java.time.Duration;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* Created with IDEA
|
|
@@ -141,26 +146,30 @@ public class ComPdfKitClient {
|
|
|
*/
|
|
|
public UploadFileResult uploadFile(File file, String taskId, String password) {
|
|
|
String url = address.concat(ComPdfKitConstant.API_V1_UPLOAD_FILE);
|
|
|
- Map<String, Object> param = new HashMap<>(3);
|
|
|
- param.put("file", file);
|
|
|
- param.put("taskId", taskId);
|
|
|
- param.put("password", password);
|
|
|
- ResponseEntity<UploadFileResult> response;
|
|
|
+ MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();;
|
|
|
+ FileSystemResource fs = new FileSystemResource(file);
|
|
|
+ param.add("file", fs);
|
|
|
+ param.add("taskId", taskId);
|
|
|
+ param.add("password", password);
|
|
|
+ HttpHeaders headers = basicHeaders() ;
|
|
|
+ headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
+ ResponseEntity<ComPdfKitResponse<UploadFileResult>> response;
|
|
|
+ ParameterizedTypeReference<ComPdfKitResponse<UploadFileResult>> typeRef = new ParameterizedTypeReference<ComPdfKitResponse<UploadFileResult>>() {};
|
|
|
try {
|
|
|
response = restTemplate.exchange(
|
|
|
url,
|
|
|
HttpMethod.POST,
|
|
|
- new HttpEntity<>(JsonUtils.getJsonString(param), basicHeaders()),
|
|
|
- UploadFileResult.class
|
|
|
+ new HttpEntity<>(param, headers),
|
|
|
+ typeRef
|
|
|
);
|
|
|
} catch (Exception e) {
|
|
|
log.error(ComPdfKitConstant.EXCEPTION_MSG_UPLOAD_FILE_FAIL + "{}", e.getMessage());
|
|
|
throw new BackendRuntimeException(ComPdfKitConstant.EXCEPTION_MSG_UPLOAD_FILE_FAIL + e.getMessage());
|
|
|
}
|
|
|
- if (response.getStatusCode() != HttpStatus.OK) {
|
|
|
- throw new BackendRuntimeException(ComPdfKitConstant.EXCEPTION_MSG_UPLOAD_FILE_FAIL);
|
|
|
+ if (response.getStatusCode() != HttpStatus.OK || ObjectUtils.isEmpty(response.getBody()) || !CommonConstant.SUCCESS_CODE.equals(response.getBody().getCode()) || ObjectUtils.isEmpty(response.getBody())) {
|
|
|
+ throw new BackendRuntimeException(ComPdfKitConstant.EXCEPTION_MSG_UPLOAD_FILE_FAIL+ response.getBody().getMsg());
|
|
|
}
|
|
|
- return response.getBody();
|
|
|
+ return response.getBody().getData();
|
|
|
}
|
|
|
|
|
|
public static class ComPdfKitClientBuilder {
|