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

  平时使用Feign做微服开发调用客户端时,几乎都是普通接口调用,返回一些JSON数据,今天刚好要进行Feign客户端(服务消费者)调用服务提供者的文件下载接口,记录一下!废话不多说,直接上代码:

  首先是文件下载服务提供者的主要代码

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.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());
        }
    }
}

  服务提供者的Controller接口方法:

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

 

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

  先是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 = "provider-user")
@RequestMapping(value = "/user")
public interface UserFeignClient extends FeignClientParent {
    @GetMapping(value = "/file",consumes = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
    Response downloadFile(@RequestParam String path);
}

 

  然后是Feign客户端(服务消费者)的Controller接口方法(你可以对该方法再进行提炼,变成服务消费者通用的文件下载方法):

    @GetMapping(value = "/file")
    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.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();
        }
    }

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

  总结一下,其实原理很简单:就是将服务提供者的文件下载响应的响应体(文件内容)复制到服务消费者对外的文件下载响应体中!

posted @ 2020-07-15 13:08  岁月已走远  阅读(6327)  评论(1编辑  收藏  举报