Feign调用文件下载服务接口实例

简介: Feign调用文件下载服务接口实例

开发使用Feign做微服开发调用客户端时,几乎都是普通接口调用,返回一些JSON数据,

今天刚好要进行Feign客户端(服务消费者)调用服务提供者的文件下载接口,记录一下!代码如下:

服务提供者:

 @PostMapping(value = "/downLoadFile")
    public void downloadFile(@RequestParam String path, HttpServletResponse response) {
        File file = new File(path);
        FileUtil.fileDownload(response, file, false);
    }

FileUtil工具类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

public class FileUtil {
   private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);

   /**
    * 将指定文件对象写入到特定的Servlet响应对象,进而实现下载
    * 一般用于下载已经存在的文件
    *
    * @param response         目标Servlet响应对象
    * @param file             要下载的文件对象
    * @param isDeleteOriginal 是否删除服务器文件原本,true下载后将删除服务器上的文件
    */
   public static void fileDownload(HttpServletResponse response, File file, Boolean isDeleteOriginal) {
       try {
           InputStream inputStream = new FileInputStream(file);
           BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
           response.setContentType("multipart.form-data")
           response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
           BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());

           int length = 0;
           byte[] temp = new byte[1024 * 10];
           while ((length = bufferedInputStream.read(temp)) != -1) {
               bufferedOutputStream.write(temp, 0, length);
           }
           bufferedOutputStream.flush();
           bufferedOutputStream.close();
           bufferedInputStream.close();
           inputStream.close();
           if (isDeleteOriginal) {
               file.delete();
           }
       } catch (FileNotFoundException e) {
           e.printStackTrace();
           logger.error(e.getMessage());
       } catch (IOException e) {
           e.printStackTrace();
           logger.error(e.getMessage());
       }
   }
}

Feign客户端(服务消费者)的代码

import feign.Response;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
 
@FeignClient(name = "file-service")
public interface UserFeignClient extends FeignClientParent {
    @GetMapping(value = "/update/downLoadFile",consumes = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
    Response downloadFile(@RequestParam String path);
}

Feign客户端(服务消费者)的Controller接口方法

@GetMapping(value = "/downLoadFile")
    public void downloadFile(@RequestParam String path, HttpServletResponse response) {
        InputStream inputStream = null;
        try {
            Response serviceResponse = this.userFeignClient.downloadFile(path);
            Response.Body body = serviceResponse.body();
            inputStream = body.asInputStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            response.setContentType("multipart.form-data")
            response.setHeader("Content-Disposition", serviceResponse.headers().get("Content-Disposition").toString().replace("[","").replace("]",""));
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
            int length = 0;
            byte[] temp = new byte[1024 * 10];
            while ((length = bufferedInputStream.read(temp)) != -1) {
                bufferedOutputStream.write(temp, 0, length);
            }
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
            bufferedInputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这一段代码是为了保持服务提供者一致的文件下载输出,其中就包括文件名!

serviceResponse.headers().get("Content-Disposition").toString().replace("[","").replace("]","")

原理:就是将服务提供者的文件下载响应的响应体(文件内容)复制到服务消费者对外的文件下载响应体中

相关文章
|
4天前
|
Java API Spring
SpringBoot项目调用HTTP接口5种方式你了解多少?
SpringBoot项目调用HTTP接口5种方式你了解多少?
110 2
|
4天前
|
存储 JSON Java
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
49 2
|
4天前
|
Java Spring
SpringBoot+async异步调用接口以及几个任务同时完成和异步接口实现和调用
SpringBoot+async异步调用接口以及几个任务同时完成和异步接口实现和调用
29 0
|
7月前
|
Java API Spring
【异常】Feign 调用api模块直接进入fallback的问题解决办法
【异常】Feign 调用api模块直接进入fallback的问题解决办法
110 0
|
4天前
RestTemplate调用接口返回中文乱码
RestTemplate调用接口返回中文乱码
|
6月前
|
前端开发 Java 对象存储
使用Feign接口实现文件上传的解决方案
一般的情况下,后端有个微服务,暴露出一个文件上传的restful接口给前端,前端调用该接口获取上传后的链接以及oss key值完成上传。假设提供restful接口的这个服务叫做A,现在有个微服务B有个本地文件,需要将本地文件调用A文件文件上传接口上传到文件服务器,该如何做?
62 0
|
8月前
|
Dubbo 网络协议 Java
网关调用其他项目的方法(RPC实现)
网关调用其他项目的方法(RPC实现)
86 0
|
8月前
|
Java API Apache
springboot 调用外部接口的21种方式
springboot 调用外部接口的21种方式
464 0
|
10月前
|
Dubbo Java 应用服务中间件
SpringBoot 调用外部接口的三种方式
SpringBoot 调用外部接口的三种方式
431 1
|
10月前
|
负载均衡 Java 数据处理
案例03-fegin调用报404问题
fegin调用报404问题
102 0