You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2018/01/03 13:56:03 UTC
[incubator-servicecomb-java-chassis] branch master updated: JAV-601
fix bug:vertx server file upload, can not get client file name
This is an automated email from the ASF dual-hosted git repository.
ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new a31041e JAV-601 fix bug:vertx server file upload, can not get client file name
a31041e is described below
commit a31041e4b4bea5334d8c0465ec24a14816c14d93
Author: wujimin <wu...@huawei.com>
AuthorDate: Tue Jan 2 22:14:33 2018 +0800
JAV-601 fix bug:vertx server file upload, can not get client file name
---
.../client/CodeFirstRestTemplateSpringmvc.java | 9 +-
.../demo/springmvc/server/CodeFirstSpringmvc.java | 5 +-
.../foundation/vertx/http/FileUploadPart.java | 66 ++++++++++
.../VertxServerRequestToHttpServletRequest.java | 4 +-
.../foundation/vertx/http/TestFileUploadPart.java | 139 +++++++++++++++++++++
5 files changed, 215 insertions(+), 8 deletions(-)
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 b2e5924..d588c22 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 @@ public class CodeFirstRestTemplateSpringmvc extends CodeFirstRestTemplate {
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 96d6d8e..7176abe 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 @@ public class CodeFirstSpringmvc {
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 class CodeFirstSpringmvc {
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 0000000..5f547b2
--- /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 7bba5bd..08142a7 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 javax.ws.rs.core.HttpHeaders;
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 class VertxServerRequestToHttpServletRequest extends AbstractHttpServletR
}
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 0000000..b697065
--- /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));
+ }
+}
--
To stop receiving notification emails like this one, please contact
['"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>'].