|
@@ -0,0 +1,101 @@
|
|
|
+package cn.com.ctop.kuaishou.modules.batch.service.impl;
|
|
|
+
|
|
|
+import cn.com.ctop.common.module.utils.Check;
|
|
|
+import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
|
|
|
+import cn.com.ctop.common.module.utils.PropertiesUtils;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouMaterialUploadService;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.core.io.FileSystemResource;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class KuaiShouMaterialUploadServiceImpl implements IKuaiShouMaterialUploadService {
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ *
|
|
|
+ * @param multipartFile
|
|
|
+ * @param type
|
|
|
+ * @param accountId
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void video(MultipartFile multipartFile, String type, Long accountId, String accessToken, String code) throws IOException {
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.VIDEO_UPLOAD;
|
|
|
+ String s = uploadFile(multipartFile, "D:\\tets\\");
|
|
|
+ System.err.println(s);
|
|
|
+
|
|
|
+ File file = new File(s);
|
|
|
+
|
|
|
+ System.err.println(file);
|
|
|
+ FileSystemResource resource = new FileSystemResource(file);
|
|
|
+ Map<String, String> headerMap = new HashMap<>();
|
|
|
+ headerMap.put("Content-Type", "multipart/form-data");
|
|
|
+ headerMap.put("Access-Token", accessToken);
|
|
|
+ JSONObject requestJson = new JSONObject();
|
|
|
+ requestJson.put("signature", code);
|
|
|
+ requestJson.put("file", resource);
|
|
|
+ requestJson.put("type", type);
|
|
|
+ requestJson.put("advertiser_id", accountId);
|
|
|
+ String result = exceptInfoForRestTemplate(url, requestJson, headerMap);
|
|
|
+ System.err.println(result);
|
|
|
+
|
|
|
+ //
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String uploadFile(MultipartFile multipartFile, String dirPath) throws IOException {
|
|
|
+ String fileName = multipartFile.getOriginalFilename();
|
|
|
+ String fileSuffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
|
|
|
+ String localFileName = System.currentTimeMillis() + fileSuffix;
|
|
|
+ String filePath = dirPath + File.separator + localFileName;
|
|
|
+ File localFile = new File(filePath);
|
|
|
+ File imagePath = new File(dirPath);
|
|
|
+ if (!imagePath.exists()) {
|
|
|
+ imagePath.mkdirs();
|
|
|
+ }
|
|
|
+ multipartFile.transferTo(localFile);
|
|
|
+ return filePath;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate rest;
|
|
|
+
|
|
|
+ private String exceptInfoForRestTemplate(String url, Map<String, Object> paramMap, Map<String, String> headerMap) {
|
|
|
+ try {
|
|
|
+ MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
|
|
|
+ if (!Check.isNullMap(paramMap)) {
|
|
|
+ for (String key : paramMap.keySet()) {
|
|
|
+ param.add(key, paramMap.get(key));
|
|
|
+ }
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ if (!Check.isNullMap(headerMap)) {
|
|
|
+ for (String key : headerMap.keySet()) {
|
|
|
+ headers.add(key, headerMap.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
|
|
|
+ ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class);
|
|
|
+ return responseEntity.getBody();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|