This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 90437ee07 add a test case for upload using String with Spring MVC 
(#3419)
90437ee07 is described below

commit 90437ee076ed513493c3e932840891757cf99163
Author: liubao68 <[email protected]>
AuthorDate: Fri Oct 21 18:37:37 2022 +0800

    add a test case for upload using String with Spring MVC (#3419)
---
 .../demo/springmvc/client/TestUploadSchema.java    | 38 ++++++++++++++++++++++
 .../demo/springmvc/server/UploadSchema.java        | 21 +++++++++++-
 2 files changed, 58 insertions(+), 1 deletion(-)

diff --git 
a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestUploadSchema.java
 
b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestUploadSchema.java
index ec0cf3d4c..8acc9bdb0 100644
--- 
a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestUploadSchema.java
+++ 
b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestUploadSchema.java
@@ -20,11 +20,15 @@ package org.apache.servicecomb.demo.springmvc.client;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.UUID;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.servicecomb.demo.CategorizedTestCase;
 import org.apache.servicecomb.demo.TestMgr;
@@ -54,6 +58,8 @@ public class TestUploadSchema implements CategorizedTestCase {
     testServerStartupSuccess();
     testUploadMultiBigFiles();
     testFileUploadMultiRpc();
+    testUploadFileAndAttribute();
+    testUploadFileRequestPartAttribute();
   }
 
   private void testServerStartupSuccess() {
@@ -103,4 +109,36 @@ public class TestUploadSchema implements 
CategorizedTestCase {
     String result = fileUploadMultiInf.fileUploadMultiRpc(files);
     TestMgr.check(result, "fileUploadMulti success, and fileNum is 2");
   }
+
+  private void testUploadFileAndAttribute() throws Exception {
+    RestTemplate template = RestTemplateBuilder.create();
+    Map<String, Object> map = new HashMap<>();
+    String message = "hi";
+    File file = File.createTempFile("file", ".txt");
+    FileUtils.writeStringToFile(file, "test", StandardCharsets.UTF_8, false);
+
+    map.put("file", new FileSystemResource(file));
+    map.put("attribute", message);
+    HttpHeaders headers = new HttpHeaders();
+    
headers.setContentType(org.springframework.http.MediaType.MULTIPART_FORM_DATA);
+    String result = 
template.postForObject("servicecomb://springmvc/upload/uploadFileAndAttribute",
+        new HttpEntity<>(map, headers), String.class);
+    TestMgr.check("hi test", result);
+  }
+
+  private void testUploadFileRequestPartAttribute() throws Exception {
+    RestTemplate template = RestTemplateBuilder.create();
+    Map<String, Object> map = new HashMap<>();
+    String message = "hi";
+    File file = File.createTempFile("file", ".txt");
+    FileUtils.writeStringToFile(file, "test", StandardCharsets.UTF_8, false);
+
+    map.put("file", new FileSystemResource(file));
+    map.put("attribute", message);
+    HttpHeaders headers = new HttpHeaders();
+    
headers.setContentType(org.springframework.http.MediaType.MULTIPART_FORM_DATA);
+    String result = 
template.postForObject("servicecomb://springmvc/upload/uploadFileRequestPartAttribute",
+        new HttpEntity<>(map, headers), String.class);
+    TestMgr.check("hi test", result);
+  }
 }
diff --git 
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/UploadSchema.java
 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/UploadSchema.java
index 3880e64bc..494a4d663 100644
--- 
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/UploadSchema.java
+++ 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/UploadSchema.java
@@ -19,15 +19,19 @@ package org.apache.servicecomb.demo.springmvc.server;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.servicecomb.demo.TestMgr;
 import org.apache.servicecomb.provider.rest.common.RestSchema;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestAttribute;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestPart;
 import org.springframework.web.multipart.MultipartFile;
@@ -63,7 +67,22 @@ public class UploadSchema {
   @PostMapping(path = "/fileUploadMultiRpc", produces = 
MediaType.TEXT_PLAIN_VALUE,
       consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
   public String fileUploadMultiRpc(@RequestPart(name = "files") 
MultipartFile[] files) {
-      return "fileUploadMulti success, and fileNum is " + files.length;
+    return "fileUploadMulti success, and fileNum is " + files.length;
   }
 
+  @PostMapping(path = "/uploadFileRequestPartAttribute", produces = 
MediaType.TEXT_PLAIN_VALUE)
+  public String uploadFileRequestPartAttribute(@RequestPart(name = "file") 
MultipartFile file,
+      @RequestPart(name = "attribute") String attribute) throws IOException {
+    try (InputStream is = file.getInputStream()) {
+      return attribute + " " + IOUtils.toString(is, StandardCharsets.UTF_8);
+    }
+  }
+
+  @PostMapping(path = "/uploadFileAndAttribute", produces = 
MediaType.TEXT_PLAIN_VALUE)
+  public String uploadFileAndAttribute(@RequestPart(name = "file") 
MultipartFile file,
+      @RequestAttribute(name = "attribute") String attribute) throws 
IOException {
+    try (InputStream is = file.getInputStream()) {
+      return attribute + " " + IOUtils.toString(is, StandardCharsets.UTF_8);
+    }
+  }
 }

Reply via email to