Open Feign 动态 url 文件上传下载

1: pom

 <!-- Feign进行跨服务传递文件依赖 -->
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>

2.增加配置类

@Configuration
public class MultipartSupportConfig {
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    public Encoder feignFormEncoder(){
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }
}

3: 动态获取 url 配置类

public class DynamicFeign {

    @Autowired
    private Client feignClient;

    @Autowired
    Encoder multipartSupportConfig;

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    public <T> T getClient(Class<T> apiType,String serviceId){
        return  Feign.builder().logLevel(Logger.Level.FULL)
                .contract(new SpringMvcContract())
                .decoder(new ResponseEntityDecoder(new SpringDecoder(this.messageConverters)))
                .encoder(multipartSupportConfig)
                .requestInterceptor(new FeignConfiguration())
                .client(feignClient)
                .target(apiType, "http://"+serviceId);
    }

4 :消费者 client 

@RequestMapping("/test")
public interface TestClient {

 //导出
 @RequestMapping(value = "/{code}/templateExport", method = RequestMethod.POST)
    Response export(@RequestBody Map<String, Object> map,@PathVariable(value = "code")  String code);

// 导入
 @RequestMapping(value = "/{code}/standardImport", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    JsonResult import(@RequestPart(value = "file") MultipartFile file, @PathVariable(value = "code") String code);



}

注意:导入@RequestPart ,consumes = MediaType.MULTIPART_FORM_DATA_VALUE

5:生产者Controller

 //导出
    @RequestMapping(value = "/{code}/export", method = RequestMethod.POST)
    JsonResult export(@RequestBody Map<String, Object> map,@PathVariable(value = "code") String code,HttpServletResponse response){
        //业务代码
    }

//导入
 @RequestMapping(value = "/{code}/import", method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    JsonResult import(@RequestParam(value = "file", required = false) MultipartFile file
            , @PathVariable(value = "code") String code) {
      

    }

6:客户端调用demo

     @Autowired
    private DynamicFeign dynamicFeign;


//导出execel
 @RequestMapping(value = "/{code}/export", method = RequestMethod.POST)
    public void export(@RequestBody Map<String, Object> map
            , @PathVariable String code, HttpServletResponse httpServletResponse) {

        TestClient testClient = dynamicFeign.getClient(TestClient.class, "服务名");
        Response response = testClient.export(map, code);
     try {
            Response.Body body = response.body();
            httpServletResponse.setHeader("Content-disposition", "attachment;filename=" + code+ "-" + System.currentTimeMillis() + ".xlsx");
            InputStream fileInputStream = null;
            OutputStream outStream;
            fileInputStream = body.asInputStream();
            outStream = httpServletResponse.getOutputStream();
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes)) != -1) {
                outStream.write(bytes, 0, len);
            }
            fileInputStream.close();
            outStream.close();
            outStream.flush();
        } catch (Exception e) {
            System.out.println(e);
        }
        
    }
//导入
 @RequestMapping(value = "/{code}/import", method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    JsonResult import(@RequestParam(value = "file", required = false) MultipartFile file
            , @PathVariable(value = "code") String code) {
        TestClient testClient = dynamicFeign.getClient(TestClient.class, "服务名");
       return testClient.import(file,code);

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值