FeignMultipartSupportConfig上傳圖片配置方式
更新時間:2022年03月04日 11:42:12 作者:狼煙的煙
這篇文章主要介紹了FeignMultipartSupportConfig上傳圖片配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
FeignMultipartSupportConfig上傳圖片配置
在對應(yīng)的boot項目上關(guān)閉全局的上傳圖片的配置
@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);
? ?}
}在目標(biāo)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)對應(yīng)的服務(wù)做圖片的上傳,針對的圖片微服務(wù)就可以實現(xiàn)數(shù)據(jù)的額接收。
對應(yīng)配置文件的代碼
@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的默認(rèn)處理
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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合Java DL4J實現(xiàn)文本分類系統(tǒng)
在當(dāng)今信息爆炸的時代,自然語言處理領(lǐng)域中的文本分類顯得尤為重要,文本分類能夠高效地組織和管理海量的文本數(shù)據(jù),隨著互聯(lián)網(wǎng)的飛速發(fā)展,我們每天都被大量的文本信息所包圍,本文將介紹如何使用 Spring Boot 整合 Java Deeplearning4j 來構(gòu)建一個文本分類系統(tǒng)2024-10-10
JAVA異常信息Exception?e及e的相關(guān)方法解讀
這篇文章主要介紹了JAVA異常信息Exception?e及e的相關(guān)方法解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Mybatis-Plus默認(rèn)主鍵策略導(dǎo)致自動生成19位長度主鍵id的坑
這篇文章主要介紹了Mybatis-Plus默認(rèn)主鍵策略導(dǎo)致自動生成19位長度主鍵id的坑,本文一步步給大家分享解決方法,給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12
spring中ApplicationListener的使用小結(jié)
ApplicationListener是spring提供的一個監(jiān)聽器,本文主要介紹了spring中ApplicationListener的使用小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
2024-07-07 
