FeignMultipartSupportConfig上傳圖片配置方式
更新時間:2022年03月04日 11:42:12 作者:狼煙的煙
這篇文章主要介紹了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);
? ?
}就可以實現(xiàn)對應的服務做圖片的上傳,針對的圖片微服務就可以實現(xiàn)數(shù)據(jù)的額接收。
對應配置文件的代碼
@Configuration
public 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<String, Object> 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<String, Object> data = Collections.singletonMap("imgList", object);
? ? ? ? ? ? super.encode(data, MAP_STRING_WILDCARD, template);
? ? ? ? ? ? return;
? ? ? ? ?}
? ? ? }
? ? ? super.encode(object, bodyType, template);
? ?}
}如何使用Feign上傳圖片
添加依賴,支持SpringEncoder
? ? ? ? <dependency> ? ? ? ? ? ? <groupId>io.github.openfeign.form</groupId> ? ? ? ? ? ? <artifactId>feign-form</artifactId> ? ? ? ? ? ? <version>3.4.1</version> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>io.github.openfeign.form</groupId> ? ? ? ? ? ? <artifactId>feign-form-spring</artifactId> ? ? ? ? ? ? <version>3.4.1</version> ? ? ? ? </dependency>
將SpringFormEncoder的默認處理
encoder配置為SpringEncoder
@Configuration
public class FeignMultipartSupportConfig {
? ? @Bean
? ? public Encoder multipartFormEncoder(ObjectFactory<HttpMessageConverters> 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<String> uploadImage(@RequestPart("file") MultipartFile file);
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
使用java + selenium + OpenCV破解網(wǎng)易易盾滑動驗證碼的示例
這篇文章主要介紹了使用java + selenium + OpenCV破解網(wǎng)易易盾滑動驗證碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
JAVA使用commos-fileupload實現(xiàn)文件上傳與下載實例解析
這篇文章主要介紹了JAVA使用commos-fileupload實現(xiàn)文件上傳與下載的相關資料,需要的朋友可以參考下2016-02-02
SpringBoot+WebSocket搭建簡單的多人聊天系統(tǒng)
WebSocket是一種在單個TCP連接上進行全雙工通信的協(xié)議。這是一種比較官方的說法,簡單點來說就是,在一次TCP連接中,通信的雙方可以相互通信。這篇文章主要介紹了SpringBoot+WebSocket搭建簡單的多人聊天系統(tǒng),需要的朋友可以參考下2019-10-10
詳解SpringBoot中的統(tǒng)一結果返回與統(tǒng)一異常處理
這篇文章主要將通過詳細的討論和實例演示來幫助你更好地理解和應用Spring Boot中的統(tǒng)一結果返回和統(tǒng)一異常處理,感興趣的小伙伴可以了解下2024-03-03
詳解SpringBoot中的tomcat優(yōu)化和修改
這篇文章主要介紹了詳解SpringBoot中的tomcat優(yōu)化和修改,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09

