You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/01/03 13:56:04 UTC

[GitHub] WillemJiang closed pull request #477: [JAV-601] fix bug:vertx server file upload, can not get client file name

WillemJiang closed pull request #477: [JAV-601] fix bug:vertx server file upload, can not get client file name
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/477
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/io/servicecomb/demo/springmvc/client/CodeFirstRestTemplateSpringmvc.java b/demo/demo-springmvc/springmvc-client/src/main/java/io/servicecomb/demo/springmvc/client/CodeFirstRestTemplateSpringmvc.java
index b2e59245b..d588c22a0 100644
--- a/demo/demo-springmvc/springmvc-client/src/main/java/io/servicecomb/demo/springmvc/client/CodeFirstRestTemplateSpringmvc.java
+++ b/demo/demo-springmvc/springmvc-client/src/main/java/io/servicecomb/demo/springmvc/client/CodeFirstRestTemplateSpringmvc.java
@@ -100,16 +100,19 @@ private void testUpload(RestTemplate template, String cseUrlPrefix) throws IOExc
     File someFile = File.createTempFile("upload2", ".txt");
     FileUtils.writeStringToFile(someFile, file2Content);
 
+    String expect = file1.getName() + ":" + file1Content + "\n" + someFile.getName() + ":" + file2Content;
+
     String result = testRestTemplateUpload(template, cseUrlPrefix, file1, someFile);
-    TestMgr.check(file1Content + file2Content, result);
+    TestMgr.check(expect, result);
 
     result = uploadPartAndFile.fileUpload(new FilePart(null, file1), someFile);
-    TestMgr.check(file1Content + file2Content, result);
+    TestMgr.check(expect, result);
 
+    expect = "null:" + file1Content + "\n" + someFile.getName() + ":" + file2Content;
     result = uploadStreamAndResource
         .fileUpload(new ByteArrayInputStream(file1Content.getBytes(StandardCharsets.UTF_8)),
             new PathResource(someFile.getAbsolutePath()));
-    TestMgr.check(file1Content + file2Content, result);
+    TestMgr.check(expect, result);
   }
 
   private String testRestTemplateUpload(RestTemplate template, String cseUrlPrefix, File file1, File someFile) {
diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/io/servicecomb/demo/springmvc/server/CodeFirstSpringmvc.java b/demo/demo-springmvc/springmvc-server/src/main/java/io/servicecomb/demo/springmvc/server/CodeFirstSpringmvc.java
index 96d6d8e4b..7176abecd 100644
--- a/demo/demo-springmvc/springmvc-server/src/main/java/io/servicecomb/demo/springmvc/server/CodeFirstSpringmvc.java
+++ b/demo/demo-springmvc/springmvc-server/src/main/java/io/servicecomb/demo/springmvc/server/CodeFirstSpringmvc.java
@@ -94,7 +94,8 @@ private String _fileUpload(MultipartFile file1, Part file2) {
     try (InputStream is1 = file1.getInputStream(); InputStream is2 = file2.getInputStream()) {
       String content1 = IOUtils.toString(is1);
       String content2 = IOUtils.toString(is2);
-      return content1 + content2;
+      return file1.getOriginalFilename() + ":" + content1 + "\n" +
+          file2.getSubmittedFileName() + ":" + content2;
     } catch (IOException e) {
       throw new IllegalArgumentException(e);
     }
@@ -299,7 +300,7 @@ public OutputModelForTestIgnore testModelWithIgnoreField(@RequestBody InputModel
     return new OutputModelForTestIgnore("output_id", input.getInputId(), input.getContent(), input.getInputObject(),
         input.getInputJsonObject(), input.getInputIgnoreInterface(),
         new Person("outputSomeone"), new JsonObject("{\"OutputJsonKey\" : \"OutputJsonValue\"}"), () -> {
-    });
+        });
   }
 
   @SuppressWarnings("unchecked")
diff --git a/foundations/foundation-vertx/src/main/java/io/servicecomb/foundation/vertx/http/FileUploadPart.java b/foundations/foundation-vertx/src/main/java/io/servicecomb/foundation/vertx/http/FileUploadPart.java
new file mode 100644
index 000000000..5f547b29b
--- /dev/null
+++ b/foundations/foundation-vertx/src/main/java/io/servicecomb/foundation/vertx/http/FileUploadPart.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.servicecomb.foundation.vertx.http;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.io.FileUtils;
+
+import io.servicecomb.foundation.common.part.AbstractPart;
+import io.vertx.ext.web.FileUpload;
+
+public class FileUploadPart extends AbstractPart {
+  private FileUpload fileUpload;
+
+  public FileUploadPart(FileUpload fileUpload) {
+    this.fileUpload = fileUpload;
+  }
+
+  @Override
+  public InputStream getInputStream() throws IOException {
+    return new FileInputStream(fileUpload.uploadedFileName());
+  }
+
+  @Override
+  public String getContentType() {
+    return fileUpload.contentType();
+  }
+
+  @Override
+  public String getName() {
+    return fileUpload.name();
+  }
+
+  @Override
+  public String getSubmittedFileName() {
+    return fileUpload.fileName();
+  }
+
+  @Override
+  public long getSize() {
+    return fileUpload.size();
+  }
+
+  @Override
+  public void write(String fileName) throws IOException {
+    FileUtils.copyFile(new File(fileUpload.uploadedFileName()), new File(fileName));
+  }
+}
diff --git a/foundations/foundation-vertx/src/main/java/io/servicecomb/foundation/vertx/http/VertxServerRequestToHttpServletRequest.java b/foundations/foundation-vertx/src/main/java/io/servicecomb/foundation/vertx/http/VertxServerRequestToHttpServletRequest.java
index 7bba5bde9..08142a7f1 100644
--- a/foundations/foundation-vertx/src/main/java/io/servicecomb/foundation/vertx/http/VertxServerRequestToHttpServletRequest.java
+++ b/foundations/foundation-vertx/src/main/java/io/servicecomb/foundation/vertx/http/VertxServerRequestToHttpServletRequest.java
@@ -36,7 +36,6 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import io.servicecomb.foundation.common.part.FilePart;
 import io.servicecomb.foundation.vertx.stream.BufferInputStream;
 import io.vertx.core.MultiMap;
 import io.vertx.core.buffer.Buffer;
@@ -236,7 +235,6 @@ public Part getPart(String name) throws IOException, ServletException {
     }
 
     final FileUpload fileUpload = upload.get();
-    return new FilePart(name, fileUpload.uploadedFileName())
-        .contentType(fileUpload.contentType());
+    return new FileUploadPart(fileUpload);
   }
 }
diff --git a/foundations/foundation-vertx/src/test/java/io/servicecomb/foundation/vertx/http/TestFileUploadPart.java b/foundations/foundation-vertx/src/test/java/io/servicecomb/foundation/vertx/http/TestFileUploadPart.java
new file mode 100644
index 000000000..b697065cc
--- /dev/null
+++ b/foundations/foundation-vertx/src/test/java/io/servicecomb/foundation/vertx/http/TestFileUploadPart.java
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.servicecomb.foundation.vertx.http;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.UUID;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import io.vertx.ext.web.FileUpload;
+import mockit.Expectations;
+import mockit.Mocked;
+
+public class TestFileUploadPart {
+  @Mocked
+  FileUpload fileUpload;
+
+  FileUploadPart part;
+
+  static File file;
+
+  static String content = "fileContent";
+
+  @BeforeClass
+  public static void classSetup() throws IOException {
+    file = File.createTempFile("upload", ".txt");
+    file.deleteOnExit();
+    FileUtils.writeStringToFile(file, content);
+  }
+
+  @Before
+  public void setup() {
+    part = new FileUploadPart(fileUpload);
+
+
+  }
+
+  @Test
+  public void getInputStream() throws IOException {
+    new Expectations() {
+      {
+        fileUpload.uploadedFileName();
+        result = file.getAbsolutePath();
+      }
+    };
+    try (InputStream is = part.getInputStream()) {
+      Assert.assertEquals(content, IOUtils.toString(is));
+    }
+  }
+
+  @Test
+  public void getContentType() {
+    String contentType = "type";
+    new Expectations() {
+      {
+        fileUpload.contentType();
+        result = contentType;
+      }
+    };
+
+    Assert.assertEquals(contentType, part.getContentType());
+  }
+
+  @Test
+  public void getName() {
+    String name = "pName";
+    new Expectations() {
+      {
+        fileUpload.name();
+        result = name;
+      }
+    };
+
+    Assert.assertEquals(name, part.getName());
+  }
+
+  @Test
+  public void getSubmittedFileName() {
+    String clientName = "clientName";
+    new Expectations() {
+      {
+        fileUpload.fileName();
+        result = clientName;
+      }
+    };
+
+    Assert.assertEquals(clientName, part.getSubmittedFileName());
+  }
+
+  @Test
+  public void getSize() {
+    long fileSize = 10;
+    new Expectations() {
+      {
+        fileUpload.size();
+        result = fileSize;
+      }
+    };
+
+    Assert.assertEquals(fileSize, part.getSize());
+  }
+
+  @Test
+  public void write() throws IOException {
+    new Expectations() {
+      {
+        fileUpload.uploadedFileName();
+        result = file.getAbsolutePath();
+      }
+    };
+
+    File targetFile = new File(UUID.randomUUID().toString());
+    targetFile.deleteOnExit();
+    part.write(targetFile.getAbsolutePath());
+    Assert.assertEquals(content, FileUtils.readFileToString(targetFile));
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services