Feign does not support file transfer directly in Spring Cloud, but it can be done by introducing Feign’s extension pack.

Original: http://blog.didispace.com/spring-cloud-starter-dalston-2-4/

Service Provider (Receiving documents)

The implementation of the service provider is relatively simple, just follow the normal implementation of Spring MVC, for example:

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

    @RestController
    public class UploadController {

        @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
            returnfile.getName(); }}public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args); }}Copy the code

Service consumer (sending file)

The Feign client will be used on the service consumer side, so we need to introduce Feign’s dependency on form submission as follows:

<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>
Copy the code

Define the file uploader’s application main class and FeignClient, assuming the service provider’s service name is Eureka-feign-upload-server

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args); }}@FeignClient(value = "upload-server", configuration = UploadService.MultipartSupportConfig.class)
public interface UploadService {
 
    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String handleFileUpload(@RequestPart(value = "file") MultipartFile file);
 
    @Configuration
    class MultipartSupportConfig {
        @Bean
        public Encoder feignFormEncoder(a) {
            return newSpringFormEncoder(); }}}Copy the code

After starting the service provider, try writing test cases on the service consumer to pass files through the Feign client defined above, such as:

@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UploadTester {

    @Autowired
    private UploadService uploadService;

    @Test
    @SneakyThrows
    public void testHandleFileUpload(a) {

        File file = new File("upload.txt");
        DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file",
                MediaType.TEXT_PLAIN_VALUE, true, file.getName());

        try (InputStream input = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()) {
            IOUtils.copy(input, os);
        } catch (Exception e) {
            throw new IllegalArgumentException("Invalid file: " + e, e);
        }

        MultipartFile multi = newCommonsMultipartFile(fileItem); log.info(uploadService.handleFileUpload(multi)); }}Copy the code

Complete example:

Readers can choose from the following two repositories to view the eureka-Feign-upload-server and Eureka-feign-upload-client projects:

  • Github:https://github.com/dyc87112/SpringCloud-Learning/
  • Gitee:https://gitee.com/didispace/SpringCloud-Learning/

If you are interested in these, welcome to star, follow, favorites, forward to give support!