From 4131ead3593e81de87f8bfa62e730af58a8ee8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=95=AC=E4=BC=9F?= Date: Fri, 19 Jul 2019 11:06:52 +0800 Subject: [PATCH] =?UTF-8?q?fix=20bug:=20when=20upload=20MultipartFile[]=20?= =?UTF-8?q?=EF=BC=8Conly=20one=20can=20reach=20server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../feign/form/spring/SpringFormEncoder.java | 5 +- .../java/feign/form/feign/spring/Client.java | 7 +++ .../java/feign/form/feign/spring/Server.java | 53 ++++++++++++------- .../feign/spring/SpringFormEncoderTest.java | 9 ++++ 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/feign-form-spring/src/main/java/feign/form/spring/SpringFormEncoder.java b/feign-form-spring/src/main/java/feign/form/spring/SpringFormEncoder.java index 2ce3321..f170f3b 100644 --- a/feign-form-spring/src/main/java/feign/form/spring/SpringFormEncoder.java +++ b/feign-form-spring/src/main/java/feign/form/spring/SpringFormEncoder.java @@ -29,6 +29,7 @@ import feign.form.MultipartFormContentProcessor; import lombok.val; +import org.springframework.util.LinkedMultiValueMap; import org.springframework.web.multipart.MultipartFile; /** @@ -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(files.length, 1.F); + val data = new LinkedMultiValueMap(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)) { diff --git a/feign-form-spring/src/test/java/feign/form/feign/spring/Client.java b/feign-form-spring/src/test/java/feign/form/feign/spring/Client.java index d116fac..950ad68 100644 --- a/feign-form-spring/src/test/java/feign/form/feign/spring/Client.java +++ b/feign-form-spring/src/test/java/feign/form/feign/spring/Client.java @@ -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, diff --git a/feign-form-spring/src/test/java/feign/form/feign/spring/Server.java b/feign-form-spring/src/test/java/feign/form/feign/spring/Server.java index 953a0b1..9355f0a 100644 --- a/feign-form-spring/src/test/java/feign/form/feign/spring/Server.java +++ b/feign-form-spring/src/test/java/feign/form/feign/spring/Server.java @@ -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, @@ -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, @@ -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, @@ -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()); @@ -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 upload6 (@RequestParam("popa1") MultipartFile popa1, @RequestParam("popa2") MultipartFile popa2 @@ -134,9 +134,26 @@ public ResponseEntity 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 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 download (@PathVariable("fileId") String fileId) { val multiParts = new LinkedMultiValueMap(); diff --git a/feign-form-spring/src/test/java/feign/form/feign/spring/SpringFormEncoderTest.java b/feign-form-spring/src/test/java/feign/form/feign/spring/SpringFormEncoderTest.java index 1736cfa..1658bc7 100644 --- a/feign-form-spring/src/test/java/feign/form/feign/spring/SpringFormEncoderTest.java +++ b/feign-form-spring/src/test/java/feign/form/feign/spring/SpringFormEncoderTest.java @@ -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 list = asList(