注意用到的是openfeign,自带架包,如果是feign还需要导包,并且导在发送方和feign一块
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
接收到
@PostMapping(value = "/fileupload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String fileupload(@RequestPart("file") MultipartFile file){
if(file == null){
return "附件为空,上传文件失败";
}
String filename = file.getOriginalFilename();
long fileSize = file.getSize();
return "上传成功,文件名为:"+filename+" 文件大小:"+fileSize;
}
发送方
@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
@PostMapping(value = "/fileupload",
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String fileupload(@RequestPart("file") MultipartFile file);
@Configuration
class MultipartSupportConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}
}
controller层
@RestController
public class HiController {
@Autowired
SchedualServiceHi schedualServiceHi;
@PostMapping(value = "/fileupload")
public String fileupload(@RequestPart("file") MultipartFile file){
return schedualServiceHi.fileupload(file);
}
}