Spring Cloud Feign实现文件上传下载的示例代码

 更新时间:2022年02月16日 10:07:17   作者:编程随笔  
Feign框架对于文件上传消息体格式并没有做原生支持,需要集成模块feign-form来实现,本文就详细的介绍一下如何使用,感兴趣的可以了解一下

 Feign框架对于文件上传消息体格式并没有做原生支持,需要集成模块feign-form来实现。

独立使用Feign

添加模块依赖:

<!-- Feign框架核心 -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-core</artifactId>
    <version>11.1</version>
</dependency>
<!-- 支持表单格式,文件上传格式 -->
<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form</artifactId>
    <version>3.8.0</version>
</dependency>
<!-- 文件操作工具类 -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

上传文件

定义接口:

public interface FileUploadAPI {
    // 上传文件:参数为单个文件对象
    @RequestLine("POST /test/upload/single")
    @Headers("Content-Type: multipart/form-data")
    String upload(@Param("file") File file);

    // 上传文件:参数文多个文件对象
    @RequestLine("POST /test/upload/batch")
    @Headers("Content-Type: multipart/form-data")
    String upload(@Param("files") File[] files);

    // 上传文件:参数文多个文件对象
    @RequestLine("POST /test/upload/batch")
    @Headers("Content-Type: multipart/form-data")
    String upload(@Param("files") List<File> files);

    // 上传文件:参数为文件字节数组(这种方式在服务端无法获取文件名,不要使用)
    @RequestLine("POST /test/upload/single")
    @Headers("Content-Type: multipart/form-data")
    String upload(@Param("file") byte[] bytes);

    // 上传文件:参数为FormData对象
    @RequestLine("POST /test/upload/single")
    @Headers("Content-Type: multipart/form-data")
    String upload(@Param("file") FormData photo);

    // 上传文件:参数为POJO对象
    @RequestLine("POST /test/upload/single")
    @Headers("Content-Type: multipart/form-data")
    String upload(@Param("file") MyFile myFile);

    class MyFile {
        @FormProperty("is_public")
        Boolean isPublic;
        File file;

        public Boolean getPublic() {
            return isPublic;
        }

        public void setPublic(Boolean aPublic) {
            isPublic = aPublic;
        }

        public File getFile() {
            return file;
        }

        public void setFile(File file) {
            this.file = file;
        }
    }
}

调用接口:

FileAPI fileAPI = Feign.builder()
        .encoder(new FormEncoder()) // 必须明确设置请求参数编码器
        .logger(new Slf4jLogger())
        .logLevel(Logger.Level.FULL)
        .target(FileAPI.class, "http://localhost:8080");
File file1 = new File("C:\\Users\\xxx\\Downloads\\test1.jpg");
File file2 = new File("C:\\Users\\xxx\\Downloads\\test2.jpg");

// 上传文件1:参数为文件对象
fileAPI.upload(file1);

// 上传文件2:参数为字节数组(注意:在服务端无法获取到文件名)
byte[] bytes = FileUtils.readFileToByteArray(file1);
fileAPI.upload(bytes);

// 上传文件3:参数为FormData对象
byte[] bytes = FileUtils.readFileToByteArray(file1);
FormData formData = new FormData("image/jpg", "test1.jpg", bytes);
String result = fileAPI.upload(formData);

// 上传文件4:参数为POJO对象
FileAPI.MyFile myFile = new FileAPI.MyFile();
myFile.setPublic(true);
myFile.setFile(file1);
fileAPI.upload(myFile);

// 上传文件:参数为多个文件
fileAPI.upload(new File[]{file1, file2});
fileAPI.upload(Arrays.asList(new File[]{file1, file2}));

下载文件

定义接口:

public interface FileDownloadAPI {
    // 下载文件
    @RequestLine("GET /test/download/file")
    Response download(@QueryMap Map<String, Object> queryMap);
}

调用接口:

// 下载文件时返回值为Response对象,不需要设置解码器
FileAPI fileAPI = Feign.builder()
                .logger(new Slf4jLogger())
                .logLevel(Logger.Level.FULL)
                .target(FileAPI.class, "http://localhost:8080");
String fileName = "test.jpg";
Map<String, Object> queryMap = new HashMap<>();
queryMap.put("fileName", fileName);
Response response = fileAPI.download(queryMap);
if (response.status() == 200) {
    File downloadFile = new File("D:\\Downloads\\", fileName);
    FileUtils.copyInputStreamToFile(response.body().asInputStream(), downloadFile);
}

使用Spring Cloud Feign

在Spring框架中使用Feign实现文件上传时需要依赖feign-form和feign-form-spring,这2个模块已经在“Spring Cloud Feign”中自带了,只需要添加spring-cloud-starter-openfeign依赖即可。

<!-- 集成Spring和Feign,包含了模块feign-form和feign-form-spring -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>3.0.2</version>
</dependency>

<!-- 文件操作工具类 -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

上传文件

定义接口及配置:

@FeignClient(value = "FileAPI", url = "http://localhost:8080", configuration = FileUploadAPI.FileUploadAPIConfiguration.class)
public interface FileUploadAPI {

    /**
     * 上传单个文件
     * @param file
     * @return
     */
    @RequestMapping(value = "/test/upload/single", method = RequestMethod.POST, headers = "Content-Type=multipart/form-data")
    String upload(@RequestPart("file") MultipartFile file);

    /**
     * 上传多个文件
     * @param files
     * @return
     */
    @RequestMapping(value = "/test/upload/batch", method = RequestMethod.POST, headers = "Content-Type=multipart/form-data")
    String upload(@RequestPart("files") List<MultipartFile> files);

    class FileUploadAPIConfiguration {
        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public Encoder feignEncoder () {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }

        @Bean
        public Logger feignLogger() {
            return new Slf4jLogger();
        }

        @Bean
        public Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }
    }
}

调用接口:

// 上传单个文件
File file = new File("C:\\Users\\xxx\\Downloads\\test1.jpg");
FileInputStream fis = new FileInputStream(file);
MockMultipartFile mockMultipartFile = new MockMultipartFile("file", file.getName(), "image/jpg", fis);
this.fileUploadAPI.upload(mockMultipartFile);
fis.close();

// 上传多个文件
File file1 = new File("C:\\Users\\xxx\\Downloads\\test1.jpg");
File file2 = new File("C:\\Users\\xxx\\Downloads\\test2.jpg");
FileInputStream fis1 = new FileInputStream(file1);
FileInputStream fis2 = new FileInputStream(file2);
MockMultipartFile f1 = new MockMultipartFile("files", file1.getName(), "image/jpg", fis1);
MockMultipartFile f2 = new MockMultipartFile("files", file2.getName(), "image/jpg", fis2);
this.fileUploadAPI.upload(Arrays.asList(new MockMultipartFile[]{f1, f2}));
fis1.close();
fis2.close();

下载文件

定义接口:

@FeignClient(value = "FileDownloadAPI", url = "http://localhost:8080", configuration = FileDownloadAPI.FileDownloadAPIConfiguration.class)
public interface FileDownloadAPI {

    /**
     * 下载文件
     * @param fileName 文件名
     * @return
     */
    @RequestMapping(value = "/test/download/file", method = RequestMethod.GET)
    Response download(@RequestParam("fileName") String fileName);

    // 下载文件时返回值为Response对象,不需要设置解码器
    class FileDownloadAPIConfiguration {
        @Bean
        public Logger feignLogger() {
            return new Slf4jLogger();
        }

        @Bean
        public Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }
    }
}

调用接口:

String fileName = "test.jpg";
Response response = this.fileDownloadAPI.download(fileName);
File destFile = new File("D:\\Downloads\\", fileName);
// 使用org.apache.commons.io.FileUtils工具类将输入流中的内容转存到文件
FileUtils.copyInputStreamToFile(response.body().asInputStream(), destFile);

总结

1.Feign框架需要集成模块feign-form才能支持文件上传的消息体格式。
2.不论是独立使用Feign,还是使用Spring Cloud Feign,下载文件时的返回值都必须为feign.Response类型。

到此这篇关于 Spring Cloud Feign实现文件上传下载的示例代码的文章就介绍到这了,更多相关 Spring Cloud Feign文件上传下载内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java List简介_动力节点Java学院整理

    Java List简介_动力节点Java学院整理

    Java中可变数组的原理就是不断的创建新的数组,将原数组加到新的数组中,下文对Java List用法做了详解。需要的朋友参考下吧
    2017-05-05
  • 如何准确判断邮件地址是否存在

    如何准确判断邮件地址是否存在

    本文介绍了如何判断邮件地址是否存在的方法,具有很高的使用价值,提高了工作效率
    2015-07-07
  • Java Swing实现让窗体居中显示的方法示例

    Java Swing实现让窗体居中显示的方法示例

    这篇文章主要介绍了Java Swing实现让窗体居中显示的方法,结合实例形式分析了swing使用setBounds方法控制窗口布局的相关操作技巧,需要的朋友可以参考下
    2017-11-11
  • Java实现仿淘宝滑动验证码研究代码详解

    Java实现仿淘宝滑动验证码研究代码详解

    这篇文章主要介绍了Java实现仿淘宝滑动验证码研究代码详解的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • 深入探究SpringBoot可以同时处理多少请求

    深入探究SpringBoot可以同时处理多少请求

    SpringBoot是一款非常流行的Java后端框架,它可以帮助开发人员快速构建高效的Web应用程序,但是,许多人对于SpringBoot能够同时处理多少请求的疑问仍然存在,在本篇文章中,我们将深入探讨这个问题,需要的朋友可以参考下
    2023-07-07
  • Spring计时器stopwatch使用详解

    Spring计时器stopwatch使用详解

    这篇文章主要介绍了Spring计时器stopwatch使用详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • SpringBoot任意版本集成Swagger各种版本的操作指南

    SpringBoot任意版本集成Swagger各种版本的操作指南

    在学习Swagger生成API文档的时候经常会遇到问题,而目前市面上大部分技术分享者的SpringBoot版本并没和我们的同步,导致一些一模一样的代码,在我们的项目上却无法使用,这是一个经常性的问题,本文章就旨在和大家搞定SpringBoot任意版本集成Swagger各种版本
    2024-07-07
  • Java instanceof关键字用法详解及注意事项

    Java instanceof关键字用法详解及注意事项

    instanceof 是 Java 的保留关键字。它的作用是测试它左边的对象是否是它右边的类的实例,返回 boolean 的数据类型。本文重点给大家介绍Java instanceof关键字用法详解及注意事项,需要的朋友参考下吧
    2021-09-09
  • Java设计模式之动态代理

    Java设计模式之动态代理

    今天小编就为大家分享一篇关于Java设计模式之动态代理,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • Java中使用print、printf、println的示例及区别

    Java中使用print、printf、println的示例及区别

    Java 的输出方式一般有这三种,print、println、printf,它们都是 java.long 包里的System类中的方法,本文重点给大家介绍Java中使用print、printf、println的示例,需要的朋友可以参考下
    2023-05-05

最新评论