Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize the function of uploading multiple files with Feign

Shulou Source: shulou.com Published: 2022-06-02 09:38:45 10月02日 Update

This article shows you how to use Feign to achieve multi-file upload function, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

If you want to use Feign to upload multiple files in batch, the Controller implementation is as follows:

@ PostMapping (value = "/ uploadBatch/ali", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, headers = "content-type=multipart/form-data") @ ApiOperation (value = "Ali Cloud batch upload files") @ ResponseBody public Response uploadBatchAli (@ RequestPart ("files") MultipartFile [] files @ RequestParam ("path") String path) {return fileServiceClient.uploadBatchAli (files, path) }

The FeignClient code is as follows

@ PostMapping (value = File_Service_API_PREFIX + "/ uploadBatch/ali", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) Response uploadBatchAli (@ RequestPart ("files") MultipartFile [] files, @ RequestParam ("path") String path)

When Feign invokes the service and transmits something like MultipartFile [] files, the following error occurs

Could not write request: no suitable HttpMessageConverter found for request type [[Lorg.springframework.web.multipart.MultipartFile;] and content type [multipart/form-data] "

The error is due to a problem with Feign when assembling MultipartFile [] files. To solve this problem, you can rewrite the SpringFormEncoder class. The rewritten code is as follows

Public class SpringMultipartEncoder extends SpringFormEncoder {public SpringMultipartEncoder (Encoder delegate) {super (delegate); MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor (MULTIPART); processor.addWriter (new SpringSingleMultipartFileWriter ()); processor.addWriter (new SpringManyMultipartFilesWriter ()) } @ Override public void encode (Object object, Type bodyType, RequestTemplate template) throws EncodeException {if (bodyType! = null & & bodyType.equals (MultipartFile [] .class)) {MultipartFile [] file = (MultipartFile []) object If (file! = null) {Map data = Collections.singletonMap (file.length = = 0? "": file [0] .getName (), object); super.encode (data, MAP_STRING_WILDCARD, template); return Super.encode (object, bodyType, template);}}

Then the configuration classes are as follows:

@ Configurationpublic class MultipartSupportConfig {@ Autowired private ObjectFactory messageConverters; @ Bean public Encoder feignFormEncoder () {return new SpringMultipartEncoder (new SpringEncoder (messageConverters));}

Then FeignClient specifies the calling class

@ FeignClient (name = "xxx", configuration = MultipartSupportConfig.class) public interface FileServiceClient {@ PostMapping (value = File_Service_API_PREFIX + "/ uploadBatch/ali", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) Response uploadBatchAli (@ RequestPart ("files") MultipartFile [] files, @ RequestParam ("path") String path) } the above content is how to use Feign to achieve multi-file upload function, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Documents codes functions content skills time knowledge errors problems conciseness brevity that is articles more articles industries information information channels channels Ali Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information vpn Huawei Microsoft OPPO Reno