• SpringBoot集成OpenFeign实现文件下载


    目录

    前言

    一、背景介绍

    二、集成

    1.引入OpenFeign依赖

    2.各模块代码示例

    2.1 provider

    2.2 api

    2.3 consumer

    总结


    前言

    Openfeign是一种声明式,模板化的http客户端,只需创建一个接口并添加注解就可以使用,它还整合了Ribbon实现负载均衡。


    一、背景介绍

    项目包含三个模块(module):consumer,api,provider。模拟的业务场景是consumer通过调用api模块提供的接口来调用provider实现文件下载。

    consumer模块:依赖api模块,并通过调用api模块提供的接口来调用provider接口

    api模块: 封装FeignClient接口

    provider模块: 业务实现类,提供文件下载的接口

    二、集成

    1.引入OpenFeign依赖

    pom.xml

    1. <dependency>
    2. <groupId>org.springframework.cloudgroupId>
    3. <artifactId>spring-cloud-starter-openfeignartifactId>
    4. <version>2.2.1.RELEASEversion>
    5. dependency>

    2.各模块代码示例

    2.1 provider

    作为服务提供方,提供一个服务下载的接口

    1. @GetMapping("test/download/file")
    2. public void testDowanload(@RequestParam String filename, HttpServletResponse response) {
    3. File file = new File("E:/ZTest/test.txt");
    4. String fileName = "test";
    5. response.setHeader("content-type","application/octet-stream");
    6. response.setContentType("application/octet-stream");
    7. try {
    8. response.setHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(fileName+ ".txt","UTF-8"));
    9. } catch (UnsupportedEncodingException e) {
    10. //throw new Exception("错误信息");
    11. }
    12. byte[] buff = new byte[1024];
    13. FileInputStream bis = null;
    14. OutputStream os = null;
    15. try {
    16. os = response.getOutputStream();
    17. bis = new FileInputStream(file);
    18. int i = bis.read(buff);
    19. while (i != -1) {
    20. os.write(buff, 0, buff.length);
    21. os.flush();
    22. i = bis.read(buff);
    23. }
    24. } catch (IOException e2){
    25. e2.printStackTrace();
    26. } finally {
    27. if(bis != null){
    28. try{
    29. bis.close();
    30. //os.close();
    31. } catch (IOException e){
    32. e.printStackTrace();
    33. }
    34. }
    35. }
    36. }

    2.2 api

    基于OpenFeign封装服务下载的接口。api层其实也可以作为一个模块集成在provider里面,此处为了清晰,我们把api单独提出来,废话不多说上api层的代码:

    1. package com.rock.demo.feign.api;
    2. @FeignClient(name = "feignTest", url = "localhost:8081/feign",
    3. configuration= DemoFeignApi.FeignAPIConfiguration.class)
    4. public interface DemoFeignApi {
    5. @GetMapping(value = "/test/download/file")
    6. Response download(@RequestParam("filename") String fileName);
    7. class FeignAPIConfiguration {
    8. @Bean
    9. public Logger feignLogger() {
    10. return new Slf4jLogger();
    11. }
    12. @Bean
    13. public Logger.Level feignLoggerLevel() {
    14. return Logger.Level.FULL;
    15. }
    16. }
    17. }

    这里有两点需要特别注意的:

    1.注意download方法的返回类型是Respone,并且是feign.Response,这是通过OpenFeign进行文件下载所必须的.

    2.通过@FeignClient注解后面的url,可以知道OpenFeign支持单独使用或与注册中心联用。

    2.3 consumer

    该模块依赖API模块,通过调用API层的接口,获取下载的文件。

    1.使用OpenFeign,要在启动类中添加@EnableFeignClients注解

    1. @SpringBootApplication
    2. @EnableFeignClients
    3. public class ConsumerApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(ConsumerApplication.class, args);
    6. }
    7. }

    2.pom.xml添加依赖

    1. <dependency>
    2. <groupId>com.rock.demogroupId>
    3. <artifactId>apiartifactId>
    4. <version>0.0.1-SNAPSHOTversion>
    5. dependency>

    3.调用OpenFeign接口实现获取文件内容

    需要注意HttpResponse的ContentType和Header,"application/octet-stream" 代表二进制数据传输.

    1. @GetMapping("/test/file/download")
    2. public void testDownload(@RequestParam String filename, HttpServletResponse httpResponse){
    3. /*设置参数*/
    4. Response feignResponse= demoFeignApi.download(filename);
    5. try {
    6. httpResponse.reset();
    7. httpResponse.setHeader("content-type","application/octet-stream");
    8. httpResponse.setContentType("application/octet-stream");
    9. httpResponse.setHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(filename+ ".txt" ,"UTF-8"));
    10. ServletOutputStream outputStream = httpResponse.getOutputStream();
    11. Response.Body body = feignResponse.body();
    12. InputStream inputStream = body.asInputStream();
    13. IOUtils.copy(inputStream,outputStream);
    14. }catch (Exception ex){
    15. ex.printStackTrace();
    16. }
    17. }

    总结

    本文介绍了如何在SpringBoot中集成Openfeign, 并以文件下载为案例,介绍了如何使用openfeign,以及使用openfeign下载文件时的注意事项。

  • 相关阅读:
    MySQL案例详解 三:MMM高可用架构及其故障切换
    《进程状态》
    1. 安装Zookeeper
    (2023,ControlNet,CFGRW,diffusion,控制组合)向文本到图像扩散模型添加条件控制
    ComfyUI进阶:Comfyroll插件 (一)
    Spring Boot 文件上传与下载
    mysql表引擎批量转换--mysql_convert_table_format
    Jmeter性能测试步骤
    反诈中心拦截网站域名措施与申诉方法
    UI自动化测试神器Playwright(Java版)(保存登录cookie,解决免登录)
  • 原文地址:https://blog.csdn.net/qaz1qaz1qaz2/article/details/126089688