最新国产好看的视频,伊人天堂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庫(kù)來(lái)做,源碼地址

具體的使用方法是加入maven依賴(lài)

<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類(lèi)

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

FeignClient接口里方法參數(shù)是文件類(lèi)型的要用@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è)問(wèn)題,就是不支持文件數(shù)組類(lèi)型,我看了源碼,發(fā)現(xiàn)源碼里底層是有對(duì)MultipartFile[]類(lèi)型的支持的,源碼中有個(gè)類(lèi)叫SpringManyMultipartFilesWriter,是專(zhuān)門(mén)針對(duì)文件數(shù)組類(lèi)型進(jìn)行操作的,但是配置到項(xiàng)目里的SpringFormEncoder類(lèi)里卻沒(méi)有對(duì)文件數(shù)組類(lèi)型的判斷,以致不能支持文件數(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類(lèi)構(gòu)造時(shí)把SpringManyMultipartFilesWriter實(shí)例添加到了處理器列表里了,但是在encode方法里又只判斷了MultipartFile類(lèi)型,沒(méi)有判斷數(shù)組類(lèi)型,這就比較奇怪了,底層有對(duì)數(shù)組的支持但上層卻缺少了相應(yīng)判斷,而且在源碼里的test包里也沒(méi)有對(duì)文件數(shù)組類(lèi)型的測(cè)試,難道只是encode方法里漏掉了?還是說(shuō)那個(gè)文件數(shù)組的支持有問(wèn)題?所以encode方法里才沒(méi)有加入對(duì)其的判斷?

于是我先試著對(duì)encode方法進(jìn)行擴(kuò)展加入對(duì)文件數(shù)組的判斷,應(yīng)該就可以支持文件數(shù)組的上傳了,于是把SpringFormEncoder類(lèi)源碼復(fù)制出來(lái)重命名為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)過(guò)測(cè)試,已經(jīng)可以支持文件數(shù)組了,完美解決。

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

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

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

相關(guān)文章

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

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

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

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

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

    SpringBoot啟動(dòng)類(lèi)@SpringBootApplication注解背后的秘密

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

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

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

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

    Jmeter是純Java開(kāi)發(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 ,簡(jiǎn)單來(lái)說(shuō),就是一套運(yùn)行在 jenkins 上的工作流框架。這篇文章主要介紹了jenkins 構(gòu)建項(xiàng)目之 pipeline基礎(chǔ)教程,需要的朋友可以參考下
    2020-07-07
  • Java中static變量能繼承嗎

    Java中static變量能繼承嗎

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

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

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

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

    本文主要介紹了Java項(xiàng)目中大批量數(shù)據(jù)查詢(xún)導(dǎo)致OOM的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(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)知識(shí),感興趣的朋友跟隨小編一起看看吧
    2021-08-08

最新評(píng)論

北宁市| 三亚市| 崇阳县| 土默特右旗| 资阳市| 时尚| 灌阳县| 泾源县| 沅陵县| 洞口县| 旬邑县| 县级市| 南召县| 平潭县| 萍乡市| 安福县| 汾阳市| 工布江达县| 祁东县| 云梦县| 来安县| 莲花县| 寿阳县| 张掖市| 三门峡市| 珲春市| 鄂托克前旗| 沁源县| 南投县| 镇远县| 平顶山市| 桐庐县| 龙里县| 绍兴县| 南部县| 民县| 浮山县| 九龙县| 固始县| 墨脱县| 江永县|