【FeignClient】feignClient跨服务下载文件

背景:服务端提供了一个下载服务,用FeignClient调用该服务时发生错误。
错误信息为:getOutputStream() has already been called for this response
此错误原因是response已经被调用
解决方法:需要用到Feign提供的Request来做一次中继操作

代码:

服务端:

    /**
     * 下载文件
     * @param filePath  文件路径
     * @param downloadName  下载后的文件名(1.txt)
     * @param response
     */
    public static void downloadFile(String filePath, String downloadName, HttpServletResponse response) throws UnsupportedEncodingException {
        response.reset();
        response.setHeader("Content-Disposition", "attachment;filename="+ new String((downloadName).getBytes(), "iso-8859-1"));
        response.setContentType("text/plain;charset=utf-8");
        File file = new File(filePath);
        InputStream in = null;
        if(file.exists()){
            try {
                OutputStream out = response.getOutputStream();
                in = new FileInputStream(file);
                byte buffer[] = new byte[1024];
                int length = 0;
                while ((length = in.read(buffer)) >= 0){
                    out.write(buffer,0,length);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(in != null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }


    }
    @GetMapping("download")
    public void downloadYjya(HttpServletResponse response) throws IOException {
        downloadFile(?, ?, response);
    }

客户端:

public interface RtYjClient {

  @GetMapping("download")
  Response downloadYjya();

}
  @GetMapping("download")
  public void downloadYjya(HttpServletResponse servletResponse) throws IOException {
    Response response = rtYjClient.downloadYjya();
    Response.Body body = response.body();
    for (Object key : response.headers().keySet()) {
      List<String> kList = (List<String>) response.headers().get(key);
      for (String val : kList) {
        servletResponse.setHeader(key.toString(), val);
      }
    }
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
      inputStream = body.asInputStream();
      outputStream = servletResponse.getOutputStream();
      byte[] b = new byte[inputStream.available()];
      inputStream.read(b);
      outputStream.write(b);
      outputStream.flush();
    } catch (IOException e) {
      System.out.println("失败了");
    }finally {
      inputStream.close();
      outputStream.close();
    }
  }

参考文章:

1,https://www.jb51.net/article/160275.htm

2,FeignClient 跨服务上传文件、导出Excel_赵丙双的博客-CSDN博客_feign 导出excel

3,Feign跨服务下载文件; getOutputStream() has already been called for this response_在你之后的博客-CSDN博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值