springcloud如何使用Feign后臺內(nèi)部傳遞MultipartFile
如何使用Feign后臺內(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);
/**
* 引用配置類MultipartSupportConfig.并且實例化
*/
class MultipartSupportConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}
}若SpringFormEncoder 引入報錯,加上下面的依賴
<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);
}注意點

Feign進(jìn)行跨服務(wù)傳遞MultipartFile文件
通過調(diào)用服務(wù)進(jìn)行文件上傳,避免每個需要上傳文件的模塊都寫一遍上傳服務(wù),造成代碼冗余。
本文主要包含通過feign進(jìn)行文件上傳模塊。
使技術(shù)人員在開發(fā)過程中遇到問題時有地可查,有章可循。
通過feign進(jìn)行跨服務(wù)傳遞MultipartFile文件
添加依賴
<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);
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
在SpringBoot中使用UniHttp簡化天地圖路徑規(guī)劃調(diào)用實踐記錄(場景分析)
本文介紹了如何在SpringBoot項目中使用UniHttp簡化天地圖路徑規(guī)劃接口的調(diào)用,通過一個具體的例子展示了如何根據(jù)中文地址獲取經(jīng)緯度坐標(biāo),并使用UniHttp調(diào)用天地圖路徑規(guī)劃服務(wù),感興趣的朋友一起看看吧2025-02-02
SpringBoot整合EasyExcel實現(xiàn)大規(guī)模數(shù)據(jù)的并行導(dǎo)出與壓縮下載
在 Spring Boot 應(yīng)用中,整合 EasyExcel 實現(xiàn)并行導(dǎo)出數(shù)據(jù)并進(jìn)行 Zip 壓縮下載可以極大地提高數(shù)據(jù)處理效率和用戶體驗,文中通過代碼示例介紹的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2024-10-10
Spring Eureka 未授權(quán)訪問漏洞修復(fù)問題小結(jié)
項目組使用的 Spring Boot 比較老,是 1.5.4.RELEASE ,最近被檢測出 Spring Eureka 未授權(quán)訪問漏洞,這篇文章主要介紹了Spring Eureka 未授權(quán)訪問漏洞修復(fù)問題小結(jié),需要的朋友可以參考下2024-04-04
Java動態(tài)循環(huán)隊列是如何實現(xiàn)的
今天帶大家學(xué)習(xí)java隊列的相關(guān)知識,文章圍繞著如何實現(xiàn)Java動態(tài)循環(huán)隊列展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
window?下?win10?jdk8安裝與環(huán)境變量的配置過程
這篇文章主要介紹了window?下?win10?jdk8安裝與環(huán)境變量的配置,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08

