|
|
@@ -10,19 +10,19 @@ import lombok.AllArgsConstructor;
|
|
|
import lombok.Data;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.core.io.FileSystemResource;
|
|
|
+import org.springframework.core.io.ByteArrayResource;
|
|
|
import org.springframework.http.*;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.IOException;
|
|
|
+import java.io.*;
|
|
|
import java.net.MalformedURLException;
|
|
|
import java.net.URL;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.security.MessageDigest;
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
@@ -85,7 +85,8 @@ public class HuiTaiUploadImageComponents {
|
|
|
HuaTaiImageInitParamResponse.TypeTreeNodeVoDTO.ChildrenDTO childrenDTO1 =
|
|
|
HuaTaiImageInitParamResponse.filteringTypeCode(childrenDTO, fileTypeEnum.getCode());
|
|
|
FileuploadResult fileupload = fileupload(domain, token, childrenDTO1, insUploadImagesDto.getFilePath(), file, initParamResponse.getStorageType());
|
|
|
- saveFileIdx(domain, token, initParamResponse, fileupload, fileName, childrenDTO1);
|
|
|
+ String imgId = insUploadImagesDto.getId();
|
|
|
+ saveFileIdx(domain, token, initParamResponse, fileupload, fileName, childrenDTO1, imgId);
|
|
|
map.put(insUploadImagesDto.getImageType(), insUploadImagesDto.getImageId());
|
|
|
} catch (Exception e) {
|
|
|
insImagesUploadCacheComponents.setUploadImageCache(companyId, map);
|
|
|
@@ -126,29 +127,47 @@ public class HuiTaiUploadImageComponents {
|
|
|
* @return 文件路劲
|
|
|
*/
|
|
|
private FileuploadResult fileupload(String domain, String token, HuaTaiImageInitParamResponse.TypeTreeNodeVoDTO.ChildrenDTO childrenDTO, String path,
|
|
|
- String fileName, String storageType) {
|
|
|
+ String fileName, String storageType) throws Exception {
|
|
|
File file = new File(path);
|
|
|
+ String md5Hash = getMD5Hash(path);
|
|
|
byte[] bytes = file2byte(file);
|
|
|
double v = (double) bytes.length / 1024;
|
|
|
- HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
- httpHeaders.setAccept(Collections.singletonList(MediaType.MULTIPART_FORM_DATA));
|
|
|
- httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
-
|
|
|
+ String uuidImagesName = UUID.nameUUIDFromBytes(bytes).toString();
|
|
|
+ // 请求参数
|
|
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
|
|
- body.set("file", new FileSystemResource(file));
|
|
|
+ ByteArrayResource byteArrayResource = new ByteArrayResource(bytes) {
|
|
|
+ @Override
|
|
|
+ public String getFilename() {
|
|
|
+ return fileName;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ HttpHeaders httpHeadersFile = new HttpHeaders();
|
|
|
+ httpHeadersFile.setContentType(MediaType.IMAGE_JPEG);
|
|
|
+ body.set("file", new HttpEntity<>(byteArrayResource, httpHeadersFile));
|
|
|
body.set("token", token);
|
|
|
body.set("bussModule", childrenDTO.getBussType());
|
|
|
body.set("bussNo", childrenDTO.getBussNo());
|
|
|
body.set("fileName", fileName);
|
|
|
- body.set("saveFileName", fileName);
|
|
|
+ body.set("saveFileName", null);
|
|
|
body.set("bucket", childrenDTO.getBucket());
|
|
|
body.set("storageType", storageType);
|
|
|
+
|
|
|
+ // 请求头
|
|
|
+ HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
+ httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
+ httpHeaders.set("Bucket", storageType);
|
|
|
+ httpHeaders.set("Bussmodule", childrenDTO.getBussType());
|
|
|
+ httpHeaders.set("Bussno", childrenDTO.getBussNo());
|
|
|
// token
|
|
|
+ httpHeaders.set("Token", token);
|
|
|
+ // 文件名称
|
|
|
+ httpHeaders.set("Filename", fileName);
|
|
|
// 文件名称
|
|
|
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(body, httpHeaders);
|
|
|
String fileuploadUrl = domain + FILE_UPLOAD;
|
|
|
ResponseEntity<String> response = restTemplate.postForEntity(fileuploadUrl, httpEntity, String.class);
|
|
|
- return new FileuploadResult(response.getBody(), v);
|
|
|
+ String imageUrl = response.getBody().replace("\"", "");
|
|
|
+ return new FileuploadResult(imageUrl, v, uuidImagesName, md5Hash);
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -162,9 +181,11 @@ public class HuiTaiUploadImageComponents {
|
|
|
*/
|
|
|
private void saveFileIdx(String domain, String token, HuaTaiImageInitParamResponse response, FileuploadResult fileupload,
|
|
|
String fileName,
|
|
|
- HuaTaiImageInitParamResponse.TypeTreeNodeVoDTO.ChildrenDTO childrenDTO) {
|
|
|
+ HuaTaiImageInitParamResponse.TypeTreeNodeVoDTO.ChildrenDTO childrenDTO,
|
|
|
+ String imgId) {
|
|
|
+
|
|
|
HuaTaiSaveFileIdxRequest huaTaiSaveFileIdxRequest = new HuaTaiSaveFileIdxRequest(token, response, fileupload, fileName,
|
|
|
- childrenDTO);
|
|
|
+ childrenDTO, imgId);
|
|
|
huaTaiSaveFileIdxRequest.getFileIdxVo().setImgId(System.currentTimeMillis());
|
|
|
HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
@@ -182,6 +203,9 @@ public class HuiTaiUploadImageComponents {
|
|
|
|
|
|
private double fileSize;
|
|
|
|
|
|
+ private String uuidImagesName;
|
|
|
+
|
|
|
+ private String md5Hash;
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -224,6 +248,29 @@ public class HuiTaiUploadImageComponents {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static String getMD5Hash(String imageFilePath) throws Exception {
|
|
|
+ InputStream fis = Files.newInputStream(Paths.get(imageFilePath));
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ MessageDigest complete = MessageDigest.getInstance("MD5");
|
|
|
+ int numRead;
|
|
|
+
|
|
|
+ do {
|
|
|
+ numRead = fis.read(buffer);
|
|
|
+ if (numRead > 0) {
|
|
|
+ complete.update(buffer, 0, numRead);
|
|
|
+ }
|
|
|
+ } while (numRead != -1);
|
|
|
+
|
|
|
+ fis.close();
|
|
|
+ byte[] b = complete.digest();
|
|
|
+
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ for (int i = 0; i < b.length; i++) {
|
|
|
+ result.append(Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1));
|
|
|
+ }
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|