In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章主要介绍FeignMultipartSupportConfig上传图片配置方式是什么,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
FeignMultipartSupportConfig上传图片配置在对应的boot项目上关闭全局的上传图片的配置@SpringBootApplication@EnableCircuitBreaker@EnableEurekaClient@EnableFeignClients@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = FeignMultipartSupportConfig.class)})public class BootstrapApplication { public static void main(String[] args) { SpringApplication.run(BootstrapApplication.class, args); }}在目标feign上面添加@FeignClient(name = "micro-picture", fallbackFactory = MicroPictureFactory.class, configuration = FeignMultipartSupportConfig.class)public interface MicroPictureClient {@RequestMapping(value = { "/picture/common/upload/{commonKey}" }, method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public String upload(@RequestPart("image") MultipartFile image, @PathVariable("commonKey") Long commonKey); }
就可以实现对应的服务做图片的上传,针对的图片微服务就可以实现数据的额接收。
对应配置文件的代码@Configurationpublic class FeignMultipartSupportConfig { @Bean @Primary @Scope("prototype") public Encoder multipartFormEncoder() { return new FeignSpringFormEncoder(); } @Bean public feign.Logger.Level multipartLoggerLevel() { return feign.Logger.Level.FULL; }}package com.zhht.config;import feign.RequestTemplate;import feign.codec.EncodeException;import feign.codec.Encoder;import feign.form.ContentType;import feign.form.FormEncoder;import feign.form.MultipartFormContentProcessor;import feign.form.spring.SpringManyMultipartFilesWriter;import feign.form.spring.SpringSingleMultipartFileWriter;import org.springframework.web.multipart.MultipartFile;import java.lang.reflect.Type;import java.util.Collections;import java.util.Map;public class FeignSpringFormEncoder extends FormEncoder { public FeignSpringFormEncoder() { this(new Default()); } public FeignSpringFormEncoder(Encoder delegate) { super(delegate); MultipartFormContentProcessor processor = (MultipartFormContentProcessor) this .getContentProcessor(ContentType.MULTIPART); processor.addWriter(new SpringSingleMultipartFileWriter()); processor.addWriter(new SpringManyMultipartFilesWriter()); } public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { if (bodyType.equals(MultipartFile.class)) { MultipartFile file = (MultipartFile) object; if (file != null) { Map data = Collections.singletonMap("image", object); super.encode(data, MAP_STRING_WILDCARD, template); return; } } else if (bodyType.equals(MultipartFile[].class)) { MultipartFile[] file = (MultipartFile[]) object; if (file != null) { Map data = Collections.singletonMap("imgList", object); super.encode(data, MAP_STRING_WILDCARD, template); return; } } super.encode(object, bodyType, template); }}如何使用Feign上传图片添加依赖,支持SpringEncoder io.github.openfeign.form feign-form 3.4.1 io.github.openfeign.form feign-form-spring 3.4.1 将SpringFormEncoder的默认处理
encoder配置为SpringEncoder
@Configurationpublic class FeignMultipartSupportConfig { @Bean public Encoder multipartFormEncoder(ObjectFactory messageConverters) { return new SpringFormEncoder(new SpringEncoder(messageConverters)); }}编写client@FeignClient(value = "****", fallbackFactory = UploadClientFallbackFactory.class , configuration = FeignMultipartSupportConfig.class)public interface UploadClient { /** * 上传图片文件 * * @param file * @return */ @PostMapping(value = "/tbk/feedback/upload", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) BaseResponse uploadImage(@RequestPart("file") MultipartFile file);}以上是"FeignMultipartSupportConfig上传图片配置方式是什么"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.