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

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


The following commit(s) were added to refs/heads/2.8.x by this push:
     new 6ccef9dfe [#4476]use more stable apis to create InputStream (#4484)
6ccef9dfe is described below

commit 6ccef9dfe6bd11ca4e6e7fb90c35220df29aabc4
Author: liubao68 <[email protected]>
AuthorDate: Sat Aug 31 15:58:19 2024 +0800

    [#4476]use more stable apis to create InputStream (#4484)
---
 .../demo/springmvc/client/TestDownloadSchema.java     | 15 +++++++++++++++
 .../demo/springmvc/client/TestUploadSchema.java       | 19 +++++++++++++++++++
 .../demo/springmvc/server/DownloadSchema.java         | 11 +++++++++--
 .../servicecomb/foundation/common/part/FilePart.java  |  4 ++--
 .../foundation/vertx/http/FileUploadPart.java         |  4 ++--
 5 files changed, 47 insertions(+), 6 deletions(-)

diff --git 
a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDownloadSchema.java
 
b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDownloadSchema.java
index 8480dc0d5..84afdad9f 100644
--- 
a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDownloadSchema.java
+++ 
b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDownloadSchema.java
@@ -30,6 +30,7 @@ import 
org.apache.servicecomb.registry.definition.DefinitionConst;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
+import org.springframework.web.client.RestOperations;
 import org.springframework.web.client.RestTemplate;
 
 import com.netflix.config.DynamicPropertyFactory;
@@ -39,6 +40,7 @@ public class TestDownloadSchema implements 
CategorizedTestCase {
   @Override
   public void testRestTransport() throws Exception {
     testDownloadFileAndDeleted();
+    testDownloadFileAndDeletedCN();
     testDownloadFileNotDeleted();
     testDownloadFileWithNull();
     testSetContentTypeByResponseEntity();
@@ -106,6 +108,19 @@ public class TestDownloadSchema implements 
CategorizedTestCase {
     TestMgr.check(exists, false);
   }
 
+  private void testDownloadFileAndDeletedCN() throws Exception {
+    RestOperations restTemplate = RestTemplateBuilder.create();
+    ReadStreamPart readStreamPart = restTemplate
+        
.getForObject("servicecomb://springmvc/download/deleteAfterFinished?content={1}&fileName={2}",
+            ReadStreamPart.class, "hello", "中文");
+    String hello = readStreamPart.saveAsString().get();
+    TestMgr.check(hello, "hello");
+
+    boolean exists = restTemplate
+        
.getForObject("servicecomb://springmvc/download/assertLastFileDeleted", 
boolean.class);
+    TestMgr.check(exists, false);
+  }
+
   private void testDownloadFileWithNull() throws Exception {
     RestTemplate restTemplate = RestTemplateBuilder.create();
     ReadStreamPart readStreamPart = restTemplate
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 8acc9bdb0..f74f629d4 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
@@ -41,6 +41,7 @@ import org.springframework.http.HttpHeaders;
 import org.springframework.stereotype.Component;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestOperations;
 import org.springframework.web.client.RestTemplate;
 
 @Component
@@ -59,6 +60,7 @@ public class TestUploadSchema implements CategorizedTestCase {
     testUploadMultiBigFiles();
     testFileUploadMultiRpc();
     testUploadFileAndAttribute();
+    testUploadFileAndAttributeCN();
     testUploadFileRequestPartAttribute();
   }
 
@@ -126,6 +128,23 @@ public class TestUploadSchema implements 
CategorizedTestCase {
     TestMgr.check("hi test", result);
   }
 
+  private void testUploadFileAndAttributeCN() throws Exception {
+    RestOperations template = RestTemplateBuilder.create();
+    Map<String, Object> map = new HashMap<>();
+    String message = "hi";
+    File file = File.createTempFile("中文名称", ".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<>();
diff --git 
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/DownloadSchema.java
 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/DownloadSchema.java
index c355d9069..bcabd9ec4 100644
--- 
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/DownloadSchema.java
+++ 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/DownloadSchema.java
@@ -57,8 +57,15 @@ public class DownloadSchema {
   }
 
   @GetMapping(path = "/deleteAfterFinished")
-  public ResponseEntity<Part> deleteAfterFinished(@RequestParam("content") 
String content) throws IOException {
-    File file = createTempFile(content);
+  public ResponseEntity<Part> deleteAfterFinished(@RequestParam("content") 
String content,
+      @RequestParam(value = "fileName", required = false) String fileName) 
throws IOException {
+    File file;
+
+    if (StringUtils.isNotEmpty(fileName)) {
+      file = createTempFile(fileName, content);
+    } else {
+      file = createTempFile(content);
+    }
 
     return ResponseEntity
         .ok()
diff --git 
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePart.java
 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePart.java
index d44c40b92..1bd9ddfd9 100644
--- 
a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePart.java
+++ 
b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePart.java
@@ -18,9 +18,9 @@
 package org.apache.servicecomb.foundation.common.part;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.file.Files;
 
 import org.apache.commons.io.FileUtils;
 
@@ -41,7 +41,7 @@ public class FilePart extends AbstractPart implements 
FilePartForSend {
 
   @Override
   public InputStream getInputStream() throws IOException {
-    return new FileInputStream(file);
+    return Files.newInputStream(file.toPath());
   }
 
   @Override
diff --git 
a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadPart.java
 
b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadPart.java
index a444495f6..9e72d64d2 100644
--- 
a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadPart.java
+++ 
b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadPart.java
@@ -18,9 +18,9 @@
 package org.apache.servicecomb.foundation.vertx.http;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.file.Files;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.servicecomb.foundation.common.part.AbstractPart;
@@ -36,7 +36,7 @@ public class FileUploadPart extends AbstractPart {
 
   @Override
   public InputStream getInputStream() throws IOException {
-    return new FileInputStream(fileUpload.uploadedFileName());
+    return Files.newInputStream(new 
File(fileUpload.uploadedFileName()).toPath());
   }
 
   @Override

Reply via email to