Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.
Closed
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 @@ -20,7 +20,9 @@
import static java.util.Collections.singletonMap;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import feign.RequestTemplate;
import feign.codec.EncodeException;
Expand Down Expand Up @@ -63,22 +65,15 @@ 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);
for (val file : files) {
data.put(file.getName(), file);
}
val data = Arrays.stream(files).collect(Collectors.groupingBy(MultipartFile::getName));
super.encode(data, MAP_STRING_WILDCARD, template);
} else if (bodyType.equals(MultipartFile.class)) {
val file = (MultipartFile) object;
val data = singletonMap(file.getName(), object);
super.encode(data, MAP_STRING_WILDCARD, template);
} else if (isMultipartFileCollection(object)) {
val iterable = (Iterable<?>) object;
val data = new HashMap<String, Object>();
for (val item : iterable) {
val file = (MultipartFile) item;
data.put(file.getName(), file);
}
val iterable = (Iterable<MultipartFile>) object;
val data = StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.groupingBy(MultipartFile::getName));
super.encode(data, MAP_STRING_WILDCARD, template);
} else {
super.encode(object, bodyType, template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ String upload4 (@PathVariable("id") String id,
)
String upload6Collection (@RequestPart List<MultipartFile> files);

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

@RequestMapping(
path = "/multipart/upload7",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
String upload7Collection (@RequestPart List<MultipartFile> files);

class ClientConfiguration {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import lombok.SneakyThrows;
Expand Down Expand Up @@ -133,6 +134,19 @@ public ResponseEntity<String> upload6 (@RequestParam("popa1") MultipartFile popa
return ResponseEntity.status(status).body(result);
}

@RequestMapping(
path = "/multipart/upload7",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
public ResponseEntity<String> upload7 (@RequestParam("files") MultipartFile[] files) throws Exception {
StringBuilder result = new StringBuilder();
for (MultipartFile kek: files) {
result.append(new String(kek.getBytes()));
}
return ResponseEntity.ok(result.toString());
}

@RequestMapping(
path = "/multipart/download/{fileId}",
method = GET,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,24 @@ public void upload6CollectionTest () throws Exception {
val response = client.upload6Collection(list);
Assert.assertEquals("Hello world", response);
}

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

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

@Test
public void upload7CollectionTest () throws Exception {
List<MultipartFile> list = asList(
new MockMultipartFile("files", "debug", null, "Hello".getBytes(UTF_8)),
new MockMultipartFile("files", "release", null, " world".getBytes(UTF_8))
);

val response = client.upload7Collection(list);
Assert.assertEquals("Hello world", response);
}
}