Spring Cloud Feign文件傳輸?shù)氖纠a
一、配置文件解析器
服務(wù)提供者和消費(fèi)者都需要配置文件解析器,這里使用 commons-fileupload 替換原有的解析器:
依賴:
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
注入 bean :
@Bean(name = "multipartResolver")
public MultipartResolver mutipartResolver(){
CommonsMultipartResolver com = new CommonsMultipartResolver();
com.setDefaultEncoding("utf-8");
return com;
}
程序入口中剔除原有的解析器:
@SpringBootApplication(exclude = {MultipartAutoConfiguration.class})
二、服務(wù)提供者,即接收文件一方的配置
Controller 的寫法:
@ResponseBody
@RequestMapping(value = "/upload", method = {RequestMethod.POST},
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Result<String> uploadFile(@RequestPart("file")MultipartFile file,
@RequestParam("id")Long id){
String fileName = file.getOriginalFilename();
String extend = FileOperateUtil.suffix(fileName);
FileOperateUtil.copy("E:\\" + fileName, file);
return ResultBuilder.success("ok");
}
@RequestPart 指定文件,后面的 @RequestParam 是額外參數(shù),注意額外參數(shù)不能超過url長(zhǎng)度限制。
三、服務(wù)消費(fè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>
文件編碼配置:
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.web.HttpMessageConverters;
import org.springframework.cloud.netflix.feign.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));
}
}
Feign 接口定義:
@FeignClient(name = "test-upload")
public interface UploadService{
@ResponseBody
@RequestMapping(value = "/upload", method = {RequestMethod.POST},
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Result<String>uploadFile(@RequestPart("file")MultipartFile file,
@RequestParam("id")Long id);
}
與普通 Feign 接口寫法差不多,注意方法注解和參數(shù)與服務(wù)提供者的 controller 一樣。
Controller 的寫法, Controller 中接收前端傳過來的文件信息和額外參數(shù),然后通過 Feign 接口傳輸?shù)竭h(yuǎn)端:
// 注入 feign 接口
@Autowired
private UploadService uploadService;
@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
@ResponseBody
public Result<String> testUpload(HttpServletRequest request, Long id){
Result<String> result = null;
MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = mRequest.getFileMap();
for (MultipartFile mFile : fileMap.values()) {
String fileName = mFile.getOriginalFilename();
result = uploadService.uploadFile(mFile, id);
}
return result;
}
四、總結(jié)
最后梳理一下流程,服務(wù)消費(fèi)者接收前端(如瀏覽器)傳過來的文件,但是并不進(jìn)行業(yè)務(wù)處理,然后通過 Feign 調(diào)用接口,把文件傳給服務(wù)提供者,服務(wù)提供者拿到文件后,進(jìn)行相應(yīng)的業(yè)務(wù)處理。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決Spring Cloud Feign 請(qǐng)求時(shí)附帶請(qǐng)求頭的問題
- SpringCloud 服務(wù)負(fù)載均衡和調(diào)用 Ribbon、OpenFeign的方法
- Spring Cloud Feign 自定義配置(重試、攔截與錯(cuò)誤碼處理) 代碼實(shí)踐
- SpringCloud Feign參數(shù)問題及解決方法
- 詳解springcloud 基于feign的服務(wù)接口的統(tǒng)一hystrix降級(jí)處理
- 詳解spring cloud feign踩坑記錄
- 詳解Spring Cloud Feign 熔斷配置的一些小坑
- Spring Cloud如何使用Feign構(gòu)造多參數(shù)的請(qǐng)求
- Spring Cloud Feign內(nèi)部實(shí)現(xiàn)代碼細(xì)節(jié)
相關(guān)文章
Spring Boot配置動(dòng)態(tài)更新問題
這篇文章主要介紹了Spring Boot配置動(dòng)態(tài)更新問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
java編程實(shí)現(xiàn)國(guó)際象棋棋盤
這篇文章主要為大家詳細(xì)介紹了java編程實(shí)現(xiàn)國(guó)際象棋棋盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
java生成字母數(shù)字組合的隨機(jī)數(shù)示例 java生成隨機(jī)數(shù)
這篇文章主要介紹了java生成字母數(shù)字組合的隨機(jī)數(shù)的示例,大家參考使用吧2014-01-01
深入解析Java類加載的案例與實(shí)戰(zhàn)教程
本篇文章主要介紹Tomcat類加載器架構(gòu),以及基于類加載和字節(jié)碼相關(guān)知識(shí),去分析動(dòng)態(tài)代理的原理,對(duì)Java類加載相關(guān)知識(shí)感興趣的朋友一起看看吧2022-05-05
IDEA中使用jclasslib插件可視化方式查看類字節(jié)碼的過程詳解
查看JAVA字節(jié)碼有兩種方式一種是使用 jdk命令 javap,還有一種就是 使用 插件了,今天給大家分享IDEA中使用jclasslib插件可視化方式查看類字節(jié)碼的過程詳解,感興趣的朋友跟隨小編一起看看吧2021-05-05
Spring循環(huán)依賴之問題復(fù)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Spring的循環(huán)依賴什么時(shí)候會(huì)出現(xiàn)以及如何解決循環(huán)依賴,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下2022-07-07
解決idea中java出現(xiàn)無效的源發(fā)行版問題
這篇文章主要給大家介紹了關(guān)于解決idea中java出現(xiàn)無效的源發(fā)行版問題的相關(guān)資料,無效的源發(fā)行版是指IntelliJ IDEA無法正確識(shí)別和處理的源代碼版本,這可能是由于錯(cuò)誤的配置、缺少依賴項(xiàng)、不兼容的插件或其他問題導(dǎo)致的,需要的朋友可以參考下2024-01-01

