Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug: when upload MultipartFile[] ,only one can reach server #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import feign.form.MultipartFormContentProcessor;

import lombok.val;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.multipart.MultipartFile;

/**
Expand Down Expand Up @@ -63,9 +64,9 @@ public SpringFormEncoder (Encoder delegate) {
public void encode (Object object, Type bodyType, RequestTemplate template) throws EncodeException {
if (bodyType.equals(MultipartFile[].class)) {
val files = (MultipartFile[]) object;
val data = new HashMap<String, Object>(files.length, 1.F);
val data = new LinkedMultiValueMap<String, MultipartFile>(files.length);
for (val file : files) {
data.put(file.getName(), file);
data.add(file.getName(), file);
}
super.encode(data, MAP_STRING_WILDCARD, template);
} else if (bodyType.equals(MultipartFile.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ String upload4 (@PathVariable("id") String id,
)
String upload6Array (@RequestPart MultipartFile[] files);

@RequestMapping(
path = "/multipart/upload6Actually",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
String upload6Actually (@RequestPart MultipartFile[] files);

@RequestMapping(
path = "/multipart/upload6",
method = POST,
Expand Down
53 changes: 35 additions & 18 deletions feign-form-spring/src/test/java/feign/form/feign/spring/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
public class Server {

@RequestMapping(
path = "/multipart/upload1/{folder}",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
path = "/multipart/upload1/{folder}",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
@SneakyThrows
public String upload1 (@PathVariable("folder") String folder,
Expand All @@ -72,9 +72,9 @@ public String upload1 (@PathVariable("folder") String folder,
}

@RequestMapping(
path = "/multipart/upload2/{folder}",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
path = "/multipart/upload2/{folder}",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
@SneakyThrows
public String upload2 (@RequestBody MultipartFile file,
Expand All @@ -85,9 +85,9 @@ public String upload2 (@RequestBody MultipartFile file,
}

@RequestMapping(
path = "/multipart/upload3/{folder}",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
path = "/multipart/upload3/{folder}",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
public String upload3 (@RequestBody MultipartFile file,
@PathVariable("folder") String folder,
Expand All @@ -105,9 +105,9 @@ public String upload4 (@PathVariable("id") String id,
}

@RequestMapping(
path = "/multipart/upload5",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
path = "/multipart/upload5",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
void upload5 (Dto dto) throws IOException {
assert "field 1 value".equals(dto.getField1());
Expand All @@ -117,9 +117,9 @@ void upload5 (Dto dto) throws IOException {
}

@RequestMapping(
path = "/multipart/upload6",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
path = "/multipart/upload6",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
public ResponseEntity<String> upload6 (@RequestParam("popa1") MultipartFile popa1,
@RequestParam("popa2") MultipartFile popa2
Expand All @@ -134,9 +134,26 @@ public ResponseEntity<String> upload6 (@RequestParam("popa1") MultipartFile popa
}

@RequestMapping(
path = "/multipart/download/{fileId}",
method = GET,
produces = MULTIPART_FORM_DATA_VALUE
path = "/multipart/upload6Actually",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
public ResponseEntity<String> upload6 (@RequestParam("files") MultipartFile[] files) throws Exception {
HttpStatus status = I_AM_A_TEAPOT;
String result = "";
if (files != null && files.length > 0) {
for (MultipartFile file: files) {
status = OK;
result += new String(file.getBytes());
}
}
return ResponseEntity.status(status).body(result);
}

@RequestMapping(
path = "/multipart/download/{fileId}",
method = GET,
produces = MULTIPART_FORM_DATA_VALUE
)
public MultiValueMap<String, Object> download (@PathVariable("fileId") String fileId) {
val multiParts = new LinkedMultiValueMap<String, Object>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ public void upload6ArrayTest () throws Exception {
Assert.assertEquals("Hello world", response);
}

@Test
public void upload6ActuallyTest () throws Exception {
val file1 = new MockMultipartFile("files", "popa1", null, "Hello".getBytes(UTF_8));
val file2 = new MockMultipartFile("files", "popa2", null, " world".getBytes(UTF_8));

val response = client.upload6Actually(new MultipartFile[] { file1, file2 });
Assert.assertEquals("Hello world", response);
}

@Test
public void upload6CollectionTest () throws Exception {
List<MultipartFile> list = asList(
Expand Down