首先需要注意的是feign post默认是json的方式
所以需要改为 consumes = MediaType.MULTIPART_FORM_DATA_VALUE 方式
一般通过Form Data上传,然后通过MultipartFile来接收文件。FeignClient本身是不支持MultipartFile,需要通过额外的配置使其支持。Spring Cloud实现起来非常简单。
步骤1
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
步骤2
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
@Configuration
public class FeignMultipartSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
@Bean
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
}
步骤3
*/
@FeignClient(value = ApiConfig.API_DASYS_SERVER_NAME,configuration = FeignMultipartSupportConfig.class)
public interface UserDasysFeign {
/**
* 上传照片
* @param fileFacePic
* @return 学生数据
*/
@PostMapping(value = ApiConfig.User.UPLOADFILE,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
int[] uploadFile(@RequestPart("fileFacePic") List<MultipartFile> fileFacePic);
}