springcloud如何使用Feign后臺(tái)內(nèi)部傳遞MultipartFile
如何使用Feign后臺(tái)內(nèi)部傳遞MultipartFile
先修改Feign Client接口
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
/**
* @author linli
* @date 20-06-27
*/
@FeignClient(value = "upload", fallbackFactory = UploadFallbackFactory.class, configuration = UploadClient.MultipartSupportConfig.class)
public interface UploadClient {
@PostMapping(path = "/upload-text", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String uploadText(@RequestPart(name = "file") MultipartFile file);
/**
* 引用配置類(lèi)MultipartSupportConfig.并且實(shí)例化
*/
class MultipartSupportConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}
}若SpringFormEncoder 引入報(bào)錯(cuò),加上下面的依賴(lài)
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.3.0</version>
</dependency>內(nèi)部調(diào)用
private String uploadFile(String str) {
FileOutputStream fos = null;
FileInputStream fis = null;
MultipartFile multipartFile = null;
byte[] bt = str.getBytes();
File file = null;
try {
file = File.createTempFile("file" + UUID.randomUUID(), ".txt");
fos = new FileOutputStream(file);
fos.write(bt, 0, bt.length);
fis = new FileInputStream(file);
multipartFile = new MockMultipartFile("file", file.getName(),
MediaType.TEXT_PLAIN_VALUE, fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return uploadClient.uploadText(multipartFile);
}注意點(diǎn)

Feign進(jìn)行跨服務(wù)傳遞MultipartFile文件
通過(guò)調(diào)用服務(wù)進(jìn)行文件上傳,避免每個(gè)需要上傳文件的模塊都寫(xiě)一遍上傳服務(wù),造成代碼冗余。
本文主要包含通過(guò)feign進(jìn)行文件上傳模塊。
使技術(shù)人員在開(kāi)發(fā)過(guò)程中遇到問(wèn)題時(shí)有地可查,有章可循。
通過(guò)feign進(jìn)行跨服務(wù)傳遞MultipartFile文件
添加依賴(lài)
<dependency> ? ?<groupId>io.github.openfeign.form</groupId> ? ?<artifactId>feign-form</artifactId> ? ?<version>3.0.3</version> </dependency> <dependency> ? ?<groupId>io.github.openfeign.form</groupId> ? ?<artifactId>feign-form-spring</artifactId> ? ?<version>3.0.3</version> </dependency>
添加配置文件
package com.ruiyi.twowayreferral.configurer;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MultipartSupportConfig {
? ?@Autowired
? ?private ObjectFactory<HttpMessageConverters> messageConverters;
? ?@Bean
? ?public Encoder feignFormEncoder() {
? ? ? ?return new SpringFormEncoder(new SpringEncoder(messageConverters));
? ?}
}代碼示例
@FeignClient(value = "controller-center")
public interface CallFrignService {
? ? /**
? ? ?* @Create 文件上傳 wanggx_ruiyi 2019.11.15
? ? ?* @param uploadPath 文件上傳地址
? ? ?* @param file 上傳的文件
? ? ?* @return
? ? ?*/
? ?@PostMapping(value = "/api/v1/common/file/fileUpload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
? ?String fileUpload(@RequestParam(value = "uploadPath", required = true) String uploadPath,@RequestPart(value = "file", required = true) MultipartFile file);
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
只需兩步實(shí)現(xiàn)Eclipse+Maven快速構(gòu)建第一個(gè)Spring Boot項(xiàng)目
這篇文章主要介紹了只需兩步實(shí)現(xiàn)Eclipse+Maven快速構(gòu)建第一個(gè)Spring Boot項(xiàng)目,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)的三種方式小結(jié)
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)的三種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring?框架的?MethodInterceptor?簡(jiǎn)介及示例代碼
MethodInterceptor接口定義了一個(gè)方法Object?intercept(Object?obj,?Method?method,?Object[]?args,?MethodProxy?proxy)?,該方法在代理對(duì)象的方法被調(diào)用時(shí)被觸發(fā),這篇文章主要介紹了Spring?框架的?MethodInterceptor?簡(jiǎn)介及示例代碼,需要的朋友可以參考下2023-09-09
如何利用 Either 和 Option 進(jìn)行函數(shù)式錯(cuò)誤處理
這篇文章主要介紹了如何利用 Either 和 Option 進(jìn)行函數(shù)式錯(cuò)誤處理。在 Java 中,錯(cuò)誤的處理在傳統(tǒng)上由異常以及創(chuàng)建和傳播異常的語(yǔ)言支持進(jìn)行。但是,如果不存在結(jié)構(gòu)化異常處理又如何呢?,需要的朋友可以參考下2019-06-06
SpringBoot整合Mybatis實(shí)現(xiàn)商品評(píng)分的項(xiàng)目實(shí)踐
本文介紹了SpringBoot整合Mybatis-plus框架實(shí)現(xiàn)對(duì)商品評(píng)分的功能實(shí)現(xiàn)流程和前端接口實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02
Spring?Validation接口入?yún)⑿r?yàn)示例代碼
Spring?Validation是一種用于實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的框架,它提供了一系列的校驗(yàn)器,針對(duì)不同的數(shù)據(jù)類(lèi)型可以使用不同的校驗(yàn)器進(jìn)行校驗(yàn),下面這篇文章主要給大家介紹了關(guān)于Spring?Validation接口入?yún)⑿r?yàn)的相關(guān)資料,需要的朋友可以參考下2023-06-06

