Skip to content

Commit

Permalink
JAV-601 fix bug:vertx server file upload, can not get client file name
Browse files Browse the repository at this point in the history
  • Loading branch information
wujimin authored and WillemJiang committed Jan 3, 2018
1 parent 4375cb5 commit a31041e
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit a31041e

Please sign in to comment.