Browse Source

公告管理附件上传代码优化

hlf 1 year ago
parent
commit
90b51d90d0

+ 64 - 10
ims-service/ims-eval/src/main/java/com/ims/eval/controller/NoticeManagementController.java

@@ -17,9 +17,8 @@ import com.ims.eval.service.IEvaluationNoticeService;
 import com.ims.eval.service.IOrganizationStructureService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.ResponseEntity;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.http.*;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
 import org.springframework.web.bind.annotation.*;
@@ -29,8 +28,10 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.servlet.http.HttpServletRequest;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
@@ -211,13 +212,26 @@ public class NoticeManagementController {
 				StringBuilder fileNames = new StringBuilder();
 				if (files.length > 0) {
 					for (MultipartFile file : files) {
-						MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
+						HttpHeaders requestHeaders = new HttpHeaders();
+						requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
+						CommonInputStreamResource commonInputStreamResource = null;
+						try {
+							commonInputStreamResource = new CommonInputStreamResource(Files.newInputStream(((File) file).toPath()), ((File) file).length(), file.getName());
+						} catch (Exception e) {
+							log.error("文件输入流转换错误", e);
+						}
+						MultiValueMap<String, Object> headers = new LinkedMultiValueMap<>();
 						headers.add("Blade-Auth", "bearer " + request.getHeader("Blade-Auth"));
 						headers.add("Content-Type", "multipart/form-data");
-						HttpEntity<LinkedMultiValueMap<String, Object>> param = new HttpEntity<>(null, headers);
-						ResponseEntity<String> responseEntity2 = restTemplate.exchange(imaConfig.getGatewayUrl() + "f-center/dm/dmDoc/save?folder={1}&objType={2}&objKey={3}&file={4}&bucketName={5}",
-							HttpMethod.POST, param, String.class, "AsApplication", "xxxt/image", "", file, "yysd");
-						log.info("\n code:{}\n header:{}\n body:{}\n", responseEntity2.getStatusCodeValue(), responseEntity2.getHeaders(), responseEntity2.getBody());
+						headers.add("folder", "AsApplication");
+						headers.add("objType", "xxxt/image");
+						headers.add("objKey", "");
+						headers.add("file", commonInputStreamResource);
+						headers.add("bucketName", "yysd");
+						HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(headers, requestHeaders);
+						ResponseEntity<String> entity = restTemplate.postForEntity(imaConfig.getGatewayUrl() + "f-center/dm/dmDoc/save", requestEntity, String.class);
+						String responseValue = (String) entity.getBody();
+						log.info("---->响应值为:" + responseValue);
 						// 上传文件路径
 						//String filePath = Path.getNoticePath();
 						// 上传并返回新文件名称
@@ -230,8 +244,8 @@ public class NoticeManagementController {
 						evaluationNotice.setNoticeAnnex(noticeAnnex);
 					} else {
 						evaluationNotice.setNoticeAnnex(newNoticeAnnex);
-					}
-					b = evaluationNoticeService.updateById(evaluationNotice);*/
+					}*/
+					b = evaluationNoticeService.updateById(evaluationNotice);
 				}
 			}
 			if (b) {
@@ -370,4 +384,44 @@ public class NoticeManagementController {
 		String httpUrl = "http://" + ip + ":28900/file/" + url;
 		return R.ok().data(httpUrl);
 	}
+
+	static class CommonInputStreamResource extends InputStreamResource {
+		private long length;
+		private String fileName;
+
+		public CommonInputStreamResource(InputStream inputStream, long length, String fileName) {
+			super(inputStream);
+			this.length = length;
+			this.fileName = fileName;
+		}
+
+		/**
+		 * 覆写父类方法
+		 * 如果不重写这个方法,并且文件有一定大小,那么服务端会出现异常
+		 * {@code The multi-part request contained parameter data (excluding uploaded files) that exceeded}
+		 */
+		@Override
+		public String getFilename() {
+			return fileName;
+		}
+
+		/**
+		 * 覆写父类 contentLength 方法
+		 * 因为 {@link org.springframework.core.io.AbstractResource#contentLength()}方法会重新读取一遍文件,
+		 * 而上传文件时,restTemplate 会通过这个方法获取大小。然后当真正需要读取内容的时候,发现已经读完,会报如下错误。
+		 */
+		@Override
+		public long contentLength() {
+			long estimate = length;
+			return estimate == 0 ? 1 : estimate;
+		}
+
+		public void setLength(long length) {
+			this.length = length;
+		}
+
+		public void setFileName(String fileName) {
+			this.fileName = fileName;
+		}
+	}
 }