question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can i upload multi file.

See original GitHub issue

In my case , i want to upload multi file ,but i get exception java.lang.IllegalStateException: Method has too many Body parameters,my code like that

@FeignClient(configuration = MultipartFormDataConfig.class)
public class Demo{
	@PostMapping(
		path = "/upload",
		consumes = org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE
	)
	Response uploadFile(
		@RequestPart MultipartFile file1,
		@RequestPart MultipartFile file2
	);
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
xxlabazacommented, Apr 17, 2019

Так, на сколько я вижу вы тут сами уже во всём разобрались…

0reactions
rojekabccommented, Nov 3, 2020

Ok. I see, that this issue is closed, but I’ll describe here a little workaround. I hope this will help someone. I resolve this in the issue, when I’s trying to send something like this via feign client:

void upload(
   @RequestPart(name="main") MultipartFile mainFile,
   @RequestPart(name="attachments") MultipartFile[] attachments);

In case of two request parts, when tries create feign client instance Method has too many Body parameters occurs. If I change them to RequestParam, than I get nothing (null).

So I do this in such way, that I create two interface API. The first version, with two RequestPart, is used on side of Controller implementation - in such way it recognizes correctly incoming parts and puts as argues. The second version, used to create feign client, looks like this

void upload(@RequestPart MultipartFile[] documents);

I’m using this with feign client configuration

    @Configuration
    class MultipartSupportConfig {

        @Bean
        public Encoder feignFormEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }

and with fix from PR: https://github.com/OpenFeign/feign-form/pull/97 - it’s needed, if for one field name (here attachments) we’d like send more than one multipart-file.

Then, if we’d like to use upload via feign client, we should call upload and create named multiparts. In this example example:

MultipartFile[] documents = new MultipartFile[3];
documents[0] = new MockMultipartFile("main", "sample-file.bin", MediaType.APPLICATION_OCTET_STREAM_VALUE, "MAIN-FILE-CONTENT".getBytes());
documents[1] = new MockMultipartFile("attachments", "attach-1.bin", MediaType.APPLICATION_OCTET_STREAM_VALUE, "ONE-CONTENT".getBytes());
documents[2] = new MockMultipartFile("attachments", "attach-2.bin", MediaType.APPLICATION_OCTET_STREAM_VALUE, "TWO-CONTENT".getBytes());
FeignClientInstance.upload(documents);

In this way multi part files names are used as field names. This will be recognized by controller and assigned to dedicated argues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Upload multiple files at once - SiteBuilder help - IT Services
Browse to the files you want to upload from your computer and use Ctrl/Cmd +select to choose multiple files. · Select Upload. ·...
Read more >
Uploading multiple files - Manual - PHP
Multiple files can be uploaded using different name for input . It is also possible to upload multiple files simultaneously and have the...
Read more >
How can I select and upload multiple files with HTML and PHP ...
How can I select and upload multiple files with HTML and PHP, using HTTP POST? ... I have experience doing this with single...
Read more >
I need to add multiple files to my online application, but there ...
You can only upload 1 file per field. If you try to upload multiple files in the same field, only 1 (the last...
Read more >
Upload Multiple Files at Once | CMS Guide - Brandeis University
Simply follow the instructions on the Uploading and Updating Files page, but instead of selecting or dragging a single file, you can select...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found