欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Cloud Feign實(shí)現(xiàn)文件上傳下載的示例代碼

 更新時(shí)間:2022年02月16日 10:07:17   作者:編程隨筆  
Feign框架對(duì)于文件上傳消息體格式并沒(méi)有做原生支持,需要集成模塊feign-form來(lái)實(shí)現(xiàn),本文就詳細(xì)的介紹一下如何使用,感興趣的可以了解一下

 Feign框架對(duì)于文件上傳消息體格式并沒(méi)有做原生支持,需要集成模塊feign-form來(lái)實(shí)現(xiàn)。

獨(dú)立使用Feign

添加模塊依賴(lài):

<!-- 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>
<!-- 文件操作工具類(lèi) -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

上傳文件

定義接口:

public interface FileUploadAPI {
? ? // 上傳文件:參數(shù)為單個(gè)文件對(duì)象
? ? @RequestLine("POST /test/upload/single")
? ? @Headers("Content-Type: multipart/form-data")
? ? String upload(@Param("file") File file);

? ? // 上傳文件:參數(shù)文多個(gè)文件對(duì)象
? ? @RequestLine("POST /test/upload/batch")
? ? @Headers("Content-Type: multipart/form-data")
? ? String upload(@Param("files") File[] files);

? ? // 上傳文件:參數(shù)文多個(gè)文件對(duì)象
? ? @RequestLine("POST /test/upload/batch")
? ? @Headers("Content-Type: multipart/form-data")
? ? String upload(@Param("files") List<File> files);

? ? // 上傳文件:參數(shù)為文件字節(jié)數(shù)組(這種方式在服務(wù)端無(wú)法獲取文件名,不要使用)
? ? @RequestLine("POST /test/upload/single")
? ? @Headers("Content-Type: multipart/form-data")
? ? String upload(@Param("file") byte[] bytes);

? ? // 上傳文件:參數(shù)為FormData對(duì)象
? ? @RequestLine("POST /test/upload/single")
? ? @Headers("Content-Type: multipart/form-data")
? ? String upload(@Param("file") FormData photo);

? ? // 上傳文件:參數(shù)為POJO對(duì)象
? ? @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;
? ? ? ? }
? ? }
}

調(diào)用接口:

FileAPI fileAPI = Feign.builder()
? ? ? ? .encoder(new FormEncoder()) // 必須明確設(shè)置請(qǐng)求參數(shù)編碼器
? ? ? ? .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:參數(shù)為文件對(duì)象
fileAPI.upload(file1);

// 上傳文件2:參數(shù)為字節(jié)數(shù)組(注意:在服務(wù)端無(wú)法獲取到文件名)
byte[] bytes = FileUtils.readFileToByteArray(file1);
fileAPI.upload(bytes);

// 上傳文件3:參數(shù)為FormData對(duì)象
byte[] bytes = FileUtils.readFileToByteArray(file1);
FormData formData = new FormData("image/jpg", "test1.jpg", bytes);
String result = fileAPI.upload(formData);

// 上傳文件4:參數(shù)為POJO對(duì)象
FileAPI.MyFile myFile = new FileAPI.MyFile();
myFile.setPublic(true);
myFile.setFile(file1);
fileAPI.upload(myFile);

// 上傳文件:參數(shù)為多個(gè)文件
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);
}

調(diào)用接口:

// 下載文件時(shí)返回值為Response對(duì)象,不需要設(shè)置解碼器
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實(shí)現(xiàn)文件上傳時(shí)需要依賴(lài)feign-form和feign-form-spring,這2個(gè)模塊已經(jīng)在“Spring Cloud Feign”中自帶了,只需要添加spring-cloud-starter-openfeign依賴(lài)即可。

<!-- 集成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>

<!-- 文件操作工具類(lèi) -->
<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 {

? ? /**
? ? ?* 上傳單個(gè)文件
? ? ?* @param file
? ? ?* @return
? ? ?*/
? ? @RequestMapping(value = "/test/upload/single", method = RequestMethod.POST, headers = "Content-Type=multipart/form-data")
? ? String upload(@RequestPart("file") MultipartFile file);

? ? /**
? ? ?* 上傳多個(gè)文件
? ? ?* @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;
? ? ? ? }
? ? }
}

調(diào)用接口:

// 上傳單個(gè)文件
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();

// 上傳多個(gè)文件
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);

? ? // 下載文件時(shí)返回值為Response對(duì)象,不需要設(shè)置解碼器
? ? class FileDownloadAPIConfiguration {
? ? ? ? @Bean
? ? ? ? public Logger feignLogger() {
? ? ? ? ? ? return new Slf4jLogger();
? ? ? ? }

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

調(diào)用接口:

String fileName = "test.jpg";
Response response = this.fileDownloadAPI.download(fileName);
File destFile = new File("D:\\Downloads\\", fileName);
// 使用org.apache.commons.io.FileUtils工具類(lèi)將輸入流中的內(nèi)容轉(zhuǎn)存到文件
FileUtils.copyInputStreamToFile(response.body().asInputStream(), destFile);

總結(jié)

1.Feign框架需要集成模塊feign-form才能支持文件上傳的消息體格式。
2.不論是獨(dú)立使用Feign,還是使用Spring Cloud Feign,下載文件時(shí)的返回值都必須為feign.Response類(lèi)型。

到此這篇關(guān)于 Spring Cloud Feign實(shí)現(xiàn)文件上傳下載的示例代碼的文章就介紹到這了,更多相關(guān) Spring Cloud Feign文件上傳下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java事務(wù)管理學(xué)習(xí)之JDBC詳解

    Java事務(wù)管理學(xué)習(xí)之JDBC詳解

    這篇文章主要介紹了Java事務(wù)管理學(xué)習(xí)之JDBC的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-03-03
  • Java實(shí)現(xiàn)順序表的增刪查改功能

    Java實(shí)現(xiàn)順序表的增刪查改功能

    這篇文章主要介紹了Java實(shí)現(xiàn)順序表的增刪查改功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • Java?中如何創(chuàng)建按鈕單擊事件

    Java?中如何創(chuàng)建按鈕單擊事件

    我們使用事件偵聽(tīng)器在Java中創(chuàng)建按鈕單擊事件,本文給大家講解Java中的按鈕單擊事件,結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • 深入Java不可變類(lèi)型的詳解

    深入Java不可變類(lèi)型的詳解

    本篇文章是Java中的不可變類(lèi)型進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Java如何實(shí)現(xiàn)通過(guò)鍵盤(pán)輸入一個(gè)數(shù)組

    Java如何實(shí)現(xiàn)通過(guò)鍵盤(pán)輸入一個(gè)數(shù)組

    這篇文章主要介紹了Java實(shí)現(xiàn)通過(guò)鍵盤(pán)輸入一個(gè)數(shù)組,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Spring之IOC詳解

    Spring之IOC詳解

    本文主要介紹了Spring中的IOC的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03
  • SpringBoot 導(dǎo)出數(shù)據(jù)生成excel文件返回方式

    SpringBoot 導(dǎo)出數(shù)據(jù)生成excel文件返回方式

    這篇文章主要介紹了SpringBoot 導(dǎo)出數(shù)據(jù)生成excel文件返回方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • java導(dǎo)出大批量(百萬(wàn)以上)數(shù)據(jù)的excel文件

    java導(dǎo)出大批量(百萬(wàn)以上)數(shù)據(jù)的excel文件

    這篇文章主要為大家詳細(xì) 介紹了java導(dǎo)出大批量即百萬(wàn)以上數(shù)據(jù)的excel文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • java實(shí)現(xiàn)字符串反轉(zhuǎn)案例

    java實(shí)現(xiàn)字符串反轉(zhuǎn)案例

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)字符串反轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • Jackson常用方法以及jacksonUtil工具類(lèi)詳解

    Jackson常用方法以及jacksonUtil工具類(lèi)詳解

    這篇文章主要介紹了Jackson常用方法以及jacksonUtil工具類(lèi)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評(píng)論