In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.