springcloud feignClient调用文件下载接口服务

A服务的文件下载接口是直接返回流文件,B服务现在需要去调用A服务,接收A服务返回的流文件的处理方式如下。

服务提供者:

服务调用方

    /**
     * 文件压缩下载
     * @param request
     */
    @PostMapping("/file/download/zip")
    Response downloadFileZip(@RequestBody BatchRenameDownloadRequest request);

 需要注意的是,这里返回值需要用  feign.Response 来接收,最后我们来看下如何对接收  的 feign.Response 进行转化

/**
     * 下载文件
     * @param response
     * @param feignResponse
     * @throws Exception
     */
    public void downLoadFile(HttpServletResponse response, Response feignResponse) throws Exception {

        InputStream inputStream = null;
        BufferedInputStream bufferedInputStream = null;
        OutputStream outputStream = null;
        try {
            // 设置ContentType,响应内容为二进制数据流,编码为utf-8,此处设定的编码是文件内容的编码
            response.setContentType("application/octet-stream;charset=utf-8");
            //这一段代码是为了保持服务提供者一致的文件下载输出,其中就包括文件名!
            response.setHeader("Content-Disposition", feignResponse.headers().get("Content-Disposition").toString().replace("[","").replace("]",""));
            inputStream = feignResponse.body().asInputStream();
            //使用这个buffereInputStream 为了防止大文件内存溢出
            bufferedInputStream = new BufferedInputStream(inputStream);
            outputStream = response.getOutputStream();
            //每次取出5M
            byte[] buffer = new byte[1024 * 1024 * 5];
            int len;
            while ((len = bufferedInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
        } catch (Exception e) {
            log.error("下载文件异常:{}", e);
        } finally {
            if (inputStream!=null){
                inputStream.close();
            }
            if (outputStream!=null){
                outputStream.close();
            }
            if (bufferedInputStream!=null){
                bufferedInputStream.close();
            }
        }
    }

经过测试,B服务调用A服务的文件下载接口,正常下载文件

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud中,可以使用@FeignClient注解来调用远程服务接口。@FeignClient注解是一个声明式的Web服务客户端,可以将一个服务接口定义成Java接口,然后使用注解的方式来调用远程服务。 具体步骤如下: 1. 引入Feign依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 创建服务接口 创建一个Java接口,用于定义远程服务接口方法。例如: ``` @FeignClient(name = "remote-service") public interface RemoteService { @GetMapping("/hello") String sayHello(); } ``` @FeignClient注解中的name属性指定了远程服务的名称,这个名称对应了服务注册中心中的服务名。 3. 调用远程服务 在需要调用远程服务的地方,通过@Autowired注入RemoteService接口实例,然后直接调用接口中的方法即可。 ``` @RestController public class MyController { @Autowired private RemoteService remoteService; @GetMapping("/test") public String test() { return remoteService.sayHello(); } } ``` 在上面的例子中,MyController通过调用RemoteService接口中的sayHello()方法来调用远程服务中的/hello接口。 需要注意的是,@FeignClient注解默认使用的是Spring MVC注解,因此在定义服务接口方法时需要使用Spring MVC的注解来指定请求方式、请求路径等信息。例如,在RemoteService接口中的sayHello()方法上使用@GetMapping注解来指定使用GET请求访问/hello接口

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值