2.4:服务消费者(Feign传文件)

一. 服务的消费方(接收文件)

  1. 编写启动类

@EnableFeignClients

@EnableDiscoveryClient

@SpringBootApplication

public class Application {

 

    public static void main(String[] args) {

        new SpringApplicationBuilder(Application.class).web(true).run(args);

    }

 

}

 

  1. 编写接收文件的Controller

@RestController

public class UploadController {

    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

    public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {

        return file.getName();

    }

}

 


二. 服务的提供方(发送文件)

  1. 添加依赖

<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>

 

  1. 编写启动类

@EnableFeignClients

@EnableDiscoveryClient

@SpringBootApplication

public class Application {

    public static void main(String[] args) {

        new SpringApplicationBuilder(Application.class).web(true).run(args);

    }

}

 

  1. 编写FeignClient接口

@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() {

            return new SpringFormEncoder();

        }

    }

 

}

 


三. 编写测试用例调用方法

 

在服务的提供者(发文件)里面写

@Slf4j

@RunWith(SpringJUnit4ClassRunner.class)

@SpringBootTest

public class UploadTester {

 

    @Autowired

    private UploadService uploadService;

 

    @Test

    @SneakyThrows

    public void testHandleFileUpload() {

 

        File file = new File("upload.txt");

        DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file",

                MediaType.TEXT_PLAIN_VALUE, true, file.getName());

 

        //()中的代码一般放的是对资源的申请,如果{}中的代码出项了异常,()中的资源就会被关闭,这在inputstream和outputstream的使用中会很方便

        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 = new CommonsMultipartFile(fileItem);

 

        log.info(uploadService.handleFileUpload(multi));

    }

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值