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

Spring Cloud中FeignClient實(shí)現(xiàn)文件上傳功能

 更新時(shí)間:2019年04月26日 09:24:18   作者:ytzzh0726  
這篇文章主要為大家詳細(xì)介紹了Spring Cloud中FeignClient實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

項(xiàng)目概況:Spring Cloud搭的微服務(wù),使用了eureka,F(xiàn)eignClient,現(xiàn)在遇到FeignClient調(diào)用接口時(shí)不支持上傳文件,

百度到兩種方案,一種是使用feign-form和feign-form-spring庫來做,源碼地址。

具體的使用方法是加入maven依賴

<dependency>
 <groupId>io.github.openfeign.form</groupId>
 <artifactId>feign-form-spring</artifactId>
 <version>3.2.2</version>
 </dependency>
 <dependency>
 <groupId>io.github.openfeign.form</groupId>
 <artifactId>feign-form</artifactId>
 <version>3.2.2</version>
</dependency>

注入SpringFormEncoder類

@Bean
 @Primary
 @Scope("prototype")
 public Encoder multipartFormEncoder() {
 return new SpringFormEncoder();
 }

FeignClient接口里方法參數(shù)是文件類型的要用@RequestPart注解,且要設(shè)置ContentType為multipart/form-data

@ResponseBody
@RequestMapping(value = "/ctstestcase/updateTestCase", method = {RequestMethod.POST}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
 Map<String, Object> updateTestCase(@RequestParam("testcaseId") String testcaseId,
 @RequestParam("name") String name, @RequestParam("assignId") String assignId,
 @RequestParam("areaId") String areaId, @RequestParam("state") Integer state,
 @RequestParam("iterationId") String iterationId,@RequestParam("priority") Integer priority,
 @RequestParam("moduleId") String moduleId, @RequestParam("executionType") Integer executionType,
 @RequestParam("summary") String summary, @RequestParam("tcsteps") String tcsteps,
 @RequestParam("relations") String relations,@RequestParam("attachments") String attachments,
 @RequestPart("files") MultipartFile[] files);

但遇到一個(gè)問題,就是不支持文件數(shù)組類型,我看了源碼,發(fā)現(xiàn)源碼里底層是有對MultipartFile[]類型的支持的,源碼中有個(gè)類叫SpringManyMultipartFilesWriter,是專門針對文件數(shù)組類型進(jìn)行操作的,但是配置到項(xiàng)目里的SpringFormEncoder類里卻沒有對文件數(shù)組類型的判斷,以致不能支持文件數(shù)組的上傳.。

SpringManyMultipartFilesWriter源碼:

@FieldDefaults(level = PRIVATE, makeFinal = true)
public class SpringManyMultipartFilesWriter extends AbstractWriter {
 
 SpringSingleMultipartFileWriter fileWriter = new SpringSingleMultipartFileWriter();
 
 @Override
 public void write (Output output, String boundary, String key, Object value) throws Exception {
 if (value instanceof MultipartFile[]) {
 val files = (MultipartFile[]) value;
 for (val file : files) {
 fileWriter.write(output, boundary, key, file);
 }
 } else if (value instanceof Iterable) {
 val iterable = (Iterable<?>) value;
 for (val file : iterable) {
 fileWriter.write(output, boundary, key, file);
 }
 }
 }
 
 @Override
 public boolean isApplicable (Object value) {
 if (value == null) {
 return false;
 }
 if (value instanceof MultipartFile[]) {
 return true;
 }
 if (value instanceof Iterable) {
 val iterable = (Iterable<?>) value;
 val iterator = iterable.iterator();
 if (iterator.hasNext() && iterator.next() instanceof MultipartFile) {
 return true;
 }
 }
 return false;
 }

SpringFormEncoder源碼:

public class SpringFormEncoder extends FormEncoder {
 
 /**
 * Constructor with the default Feign's encoder as a delegate.
 */
 public SpringFormEncoder () {
 this(new Encoder.Default());
 }
 
 /**
 * Constructor with specified delegate encoder.
 *
 * @param delegate delegate encoder, if this encoder couldn't encode object.
 */
 public SpringFormEncoder (Encoder delegate) {
 super(delegate);
 
 val processor = (MultipartFormContentProcessor) getContentProcessor(MULTIPART);
 processor.addWriter(new SpringSingleMultipartFileWriter());
 processor.addWriter(new SpringManyMultipartFilesWriter());
 }
 
 @Override
 public void encode (Object object, Type bodyType, RequestTemplate template) throws EncodeException {
 if (!bodyType.equals(MultipartFile.class)) {
 super.encode(object, bodyType, template);
 return;
 }
 
 val file = (MultipartFile) object;
 val data = singletonMap(file.getName(), object);
 super.encode(data, MAP_STRING_WILDCARD, template);
 }
}

從上面SpringFormEncoder的源碼上可以看到SpringFormEncoder類構(gòu)造時(shí)把SpringManyMultipartFilesWriter實(shí)例添加到了處理器列表里了,但是在encode方法里又只判斷了MultipartFile類型,沒有判斷數(shù)組類型,這就比較奇怪了,底層有對數(shù)組的支持但上層卻缺少了相應(yīng)判斷,而且在源碼里的test包里也沒有對文件數(shù)組類型的測試,難道只是encode方法里漏掉了?還是說那個(gè)文件數(shù)組的支持有問題?所以encode方法里才沒有加入對其的判斷?

于是我先試著對encode方法進(jìn)行擴(kuò)展加入對文件數(shù)組的判斷,應(yīng)該就可以支持文件數(shù)組的上傳了,于是把SpringFormEncoder類源碼復(fù)制出來重命名為FeignSpringFormEncoder,源碼如下:

public class FeignSpringFormEncoder extends FormEncoder {
 
 /**
 * Constructor with the default Feign's encoder as a delegate.
 */
 public FeignSpringFormEncoder() {
 this(new Encoder.Default());
 }
 
 
 /**
 * Constructor with specified delegate encoder.
 *
 * @param delegate delegate encoder, if this encoder couldn't encode object.
 */
 public FeignSpringFormEncoder(Encoder delegate) {
 super(delegate);
 
 val processor = (MultipartFormContentProcessor) getContentProcessor(MULTIPART);
 processor.addWriter(new SpringSingleMultipartFileWriter());
 processor.addWriter(new SpringManyMultipartFilesWriter());
 }
 
 
 @Override
 public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
 if (bodyType.equals(MultipartFile.class)) {
 val file = (MultipartFile) object;
 val data = singletonMap(file.getName(), object);
 super.encode(data, MAP_STRING_WILDCARD, template);
 return;
 } else if (bodyType.equals(MultipartFile[].class)) {
 val file = (MultipartFile[]) object;
 if(file != null) {
 val data = singletonMap(file.length == 0 ? "" : file[0].getName(), object);
 super.encode(data, MAP_STRING_WILDCARD, template);
 return;
 }
 }
 super.encode(object, bodyType, template);
 }
}

經(jīng)過測試,已經(jīng)可以支持文件數(shù)組了,完美解決。

這里再順便說一下當(dāng)時(shí)還百度到另一個(gè)解決文件上傳的方案,這個(gè)方案就不細(xì)說了,直接上我用到的那個(gè)開源代碼的地址

這個(gè)我試過也是可以解決文件上傳問題的,但問題是FeignClient不能用SpringMVC的注解,得用Feign自帶的注解,也因此我才擴(kuò)展了第一種方法來做的文件上傳功能。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 從基礎(chǔ)學(xué)java--數(shù)組

    從基礎(chǔ)學(xué)java--數(shù)組

    數(shù)組是相同類型數(shù)據(jù)的有序集合數(shù)組描述的是相同類型的若干個(gè)數(shù)據(jù),按照一定的先后次序排列組合而成。其中,每一個(gè)數(shù)據(jù)稱作一個(gè)數(shù)組元素,每個(gè)數(shù)組元素可以通過一個(gè)下標(biāo)來訪問它們數(shù)組的聲明創(chuàng)建
    2021-09-09
  • java集合 ArrayDeque源碼詳細(xì)分析

    java集合 ArrayDeque源碼詳細(xì)分析

    ArrayDeque是一種以數(shù)組方式實(shí)現(xiàn)的雙端隊(duì)列,它是非線程安全的。下面小編和大家一起學(xué)習(xí)一下
    2019-05-05
  • SpringBoot啟動類@SpringBootApplication注解背后的秘密

    SpringBoot啟動類@SpringBootApplication注解背后的秘密

    這篇文章主要介紹了SpringBoot啟動類@SpringBootApplication注解背后的秘密,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Java網(wǎng)絡(luò)編程基礎(chǔ)詳解

    Java網(wǎng)絡(luò)編程基礎(chǔ)詳解

    網(wǎng)絡(luò)編程是指編寫運(yùn)行在多個(gè)設(shè)備(計(jì)算機(jī))的程序,這些設(shè)備都通過網(wǎng)絡(luò)連接起來。本文介紹了一些網(wǎng)絡(luò)編程基礎(chǔ)的概念,并用Java來實(shí)現(xiàn)TCP和UDP的Socket的編程,來讓讀者更好的了解其原理
    2021-08-08
  • Jmeter環(huán)境搭建及安裝步驟

    Jmeter環(huán)境搭建及安裝步驟

    Jmeter是純Java開發(fā)的,能夠運(yùn)行Java程序的系統(tǒng)一般都可以運(yùn)行Jmeter,本文以windows下安裝步驟為例分步驟給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2021-12-12
  • jenkins 構(gòu)建項(xiàng)目之 pipeline基礎(chǔ)教程

    jenkins 構(gòu)建項(xiàng)目之 pipeline基礎(chǔ)教程

    &#8203;pipeline ,簡單來說,就是一套運(yùn)行在 jenkins 上的工作流框架。這篇文章主要介紹了jenkins 構(gòu)建項(xiàng)目之 pipeline基礎(chǔ)教程,需要的朋友可以參考下
    2020-07-07
  • Java中static變量能繼承嗎

    Java中static變量能繼承嗎

    這篇文章主要介紹了Java中static變量能繼承,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Mybatis Generator 獲取不到字段注釋的解決

    Mybatis Generator 獲取不到字段注釋的解決

    這篇文章主要介紹了Mybatis Generator 獲取不到字段注釋的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java項(xiàng)目中大批量數(shù)據(jù)查詢導(dǎo)致OOM的解決

    Java項(xiàng)目中大批量數(shù)據(jù)查詢導(dǎo)致OOM的解決

    本文主要介紹了Java項(xiàng)目中大批量數(shù)據(jù)查詢導(dǎo)致OOM的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • springmvc分層領(lǐng)域模型概念詳解

    springmvc分層領(lǐng)域模型概念詳解

    本文核心為分層領(lǐng)域模型(VO , PO , BO, DAO ,POJO等)概念的個(gè)人理解,結(jié)合springmvc淺談分層領(lǐng)域模型的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧
    2021-08-08

最新評論

万山特区| 临桂县| 西和县| 临猗县| 黄大仙区| 长兴县| 马关县| 奈曼旗| 韶关市| 攀枝花市| 车险| 台南县| 嵊州市| 兴城市| 黄浦区| 内丘县| 贵溪市| 且末县| 开远市| 胶州市| 晋宁县| 辽宁省| 财经| 陆河县| 广德县| 都安| 梧州市| 习水县| 赤城县| 繁昌县| 澄迈县| 合水县| 芦溪县| 牙克石市| 武鸣县| 黔东| 马尔康县| 汶上县| 银川市| 攀枝花市| 名山县|