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);
   
}

就可以實現對應的服務做圖片的上傳,針對的圖片微服務就可以實現數據的額接收。

對應配置文件的代碼

@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);
}

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: