转载自:Spring Cloud Feign的文件上传实现 和 Spring Cloud中FeignClient添加上传文件功能
有两种方案可以实现:
方案一:
服务消费者
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.0.3</version>
</dependency>
@RestController
@RequestMapping("checkobject")
public class CheckObjectController {
@Autowired
private ControlFeign controlFeign;
/** 上传文件 */
@PostMapping("importTestData")
public OpenResponse<Object> importTestData(@RequestPart("file")MultipartFile file) throws IOException {
if (file.isEmpty()) {
return new OpenResponse<Object>(OpenResponseCode.FAILED, "空文件");
}
ObjectRestResponse objectRestResponse = controlFeign.importTestData(file);
return objectRestResponse.isRel() ? new OpenResponse<>(OpenResponseCode.SUCCESS) : new OpenResponse<>(OpenResponseCode.FAILED, null, "调用报表世界解决出错");
}
}
@FeignClient(value = "ms-business-control-server", configuration = ControlFeign.MultipartSupportConfig.class)
public interface ControlFeign {
/** 上传文件 */
@PostMapping(value="/SoftCheckController/importTestData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ObjectRestResponse importTestData(@RequestPart(value = "file")MultipartFile file);
@Configuration
class MultipartSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
}
服务提供者
@RequestMapping(value = "/SoftCheckController")
@RestController
public class SoftCheckController {
/** 上传文件 */
@PostMapping(value="/importTestData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ObjectRestResponse importTestData(@RequestPart(value = "file") MultipartFile file) {
return softCheckService.importTestData(file);
}
}