最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

獨(dú)立使用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 {
? ? // 上傳文件:參數(shù)為單個(gè)文件對象
? ? @RequestLine("POST /test/upload/single")
? ? @Headers("Content-Type: multipart/form-data")
? ? String upload(@Param("file") File file);

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

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

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

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

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

調(diào)用接口:

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

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

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

// 上傳文件4:參數(shù)為POJO對象
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對象,不需要設(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í)需要依賴feign-form和feign-form-spring,這2個(gè)模塊已經(jīng)在“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 {

? ? /**
? ? ?* 上傳單個(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對象,不需要設(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工具類將輸入流中的內(nèi)容轉(zhuǎn)存到文件
FileUtils.copyInputStreamToFile(response.body().asInputStream(), destFile);

總結(jié)

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

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

相關(guān)文章

  • Java List簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java List簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java中可變數(shù)組的原理就是不斷的創(chuàng)建新的數(shù)組,將原數(shù)組加到新的數(shù)組中,下文對Java List用法做了詳解。需要的朋友參考下吧
    2017-05-05
  • 如何準(zhǔn)確判斷郵件地址是否存在

    如何準(zhǔn)確判斷郵件地址是否存在

    本文介紹了如何判斷郵件地址是否存在的方法,具有很高的使用價(jià)值,提高了工作效率
    2015-07-07
  • Java Swing實(shí)現(xiàn)讓窗體居中顯示的方法示例

    Java Swing實(shí)現(xiàn)讓窗體居中顯示的方法示例

    這篇文章主要介紹了Java Swing實(shí)現(xiàn)讓窗體居中顯示的方法,結(jié)合實(shí)例形式分析了swing使用setBounds方法控制窗口布局的相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • Java實(shí)現(xiàn)仿淘寶滑動(dòng)驗(yàn)證碼研究代碼詳解

    Java實(shí)現(xiàn)仿淘寶滑動(dòng)驗(yàn)證碼研究代碼詳解

    這篇文章主要介紹了Java實(shí)現(xiàn)仿淘寶滑動(dòng)驗(yàn)證碼研究代碼詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • 深入探究SpringBoot可以同時(shí)處理多少請求

    深入探究SpringBoot可以同時(shí)處理多少請求

    SpringBoot是一款非常流行的Java后端框架,它可以幫助開發(fā)人員快速構(gòu)建高效的Web應(yīng)用程序,但是,許多人對于SpringBoot能夠同時(shí)處理多少請求的疑問仍然存在,在本篇文章中,我們將深入探討這個(gè)問題,需要的朋友可以參考下
    2023-07-07
  • Spring計(jì)時(shí)器stopwatch使用詳解

    Spring計(jì)時(shí)器stopwatch使用詳解

    這篇文章主要介紹了Spring計(jì)時(shí)器stopwatch使用詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • SpringBoot任意版本集成Swagger各種版本的操作指南

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

    在學(xué)習(xí)Swagger生成API文檔的時(shí)候經(jīng)常會(huì)遇到問題,而目前市面上大部分技術(shù)分享者的SpringBoot版本并沒和我們的同步,導(dǎo)致一些一模一樣的代碼,在我們的項(xiàng)目上卻無法使用,這是一個(gè)經(jīng)常性的問題,本文章就旨在和大家搞定SpringBoot任意版本集成Swagger各種版本
    2024-07-07
  • Java instanceof關(guān)鍵字用法詳解及注意事項(xiàng)

    Java instanceof關(guān)鍵字用法詳解及注意事項(xiàng)

    instanceof 是 Java 的保留關(guān)鍵字。它的作用是測試它左邊的對象是否是它右邊的類的實(shí)例,返回 boolean 的數(shù)據(jù)類型。本文重點(diǎn)給大家介紹Java instanceof關(guān)鍵字用法詳解及注意事項(xiàng),需要的朋友參考下吧
    2021-09-09
  • Java設(shè)計(jì)模式之動(dòng)態(tài)代理

    Java設(shè)計(jì)模式之動(dòng)態(tài)代理

    今天小編就為大家分享一篇關(guān)于Java設(shè)計(jì)模式之動(dòng)態(tài)代理,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Java中使用print、printf、println的示例及區(qū)別

    Java中使用print、printf、println的示例及區(qū)別

    Java?的輸出方式一般有這三種,print、println、printf,它們都是?java.long?包里的System類中的方法,本文重點(diǎn)給大家介紹Java中使用print、printf、println的示例,需要的朋友可以參考下
    2023-05-05

最新評論

麦盖提县| 孟津县| 霍邱县| 海晏县| 五原县| 墨江| 沙雅县| 郁南县| 信阳市| 汝城县| 海安县| 萨迦县| 陆河县| 巴彦淖尔市| 当阳市| 大荔县| 烟台市| 教育| 吕梁市| 兴和县| 阜新市| 高尔夫| 全椒县| 宁阳县| 宁都县| 安陆市| 宜阳县| 太仆寺旗| 和林格尔县| 阿克苏市| 宾川县| 蓝田县| SHOW| 田阳县| 丰镇市| 毕节市| 郁南县| 通州区| 桂阳县| 什邡市| 北川|