Feign之Multipartfile⽂件传输填坑Multipartfile⽂件传输
1. 添加依赖
<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>
2. 添加配置类
@Configuration
public class FeignMultipartConfig {
@Bean
@Primary
@Scope("prototype")
public SpringFormEncoder multipartFormEncoder() {
return new SpringFormEncoder();
}
@Bean
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
}
3. 接⼝编写
@FeignClient使⽤配置类
@PostMapping设置
consumes = MediaType.MULTIPART_FORM_DATA_VALUE
使⽤@RequestPart(),不能使⽤@RequestParam()
@FeignClient(value = "face-service",configuration = FeignMultipartConfig.class)
public interface FaceClient {
@PostMapping(value = "/search/student", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResponseBase<SearchStudentFaceVO> searchStudentFace(@RequestPart("file") MultipartFile file);
}
feign不能正常传递参数MultipartFile(⽂件)的解决
之前⼯作中有个业务场景:需要把⽂件以MultipartFile进⾏服务之间的调⽤(使⽤的是Feign),特此记录下踩到的坑
需要注意的是:使⽤默认的springcloud⾃带的spring-cloud-starter-openfeign是不⽀持传递⽂件的。
我看⽹上有很多使⽤feign-form和feign-form-spring⽀持的jar包,然后配置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>
新建配置类:
package com.jsyd.figuration;
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 t.annotation.Bean;
dec.Encoder;
import feign.form.spring.SpringFormEncoder;
import t.annotation.Configuration;
/**
* 处制定了注⼊的feignFormEncoder可以覆盖掉原本的encoder
* 该⽅式可以让发送multipartFile成为可能
* @author wangyang
* @version 1.0
* @date 2021/4/13 19:13
*/
@Configuration
public class MultipartSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
配置feign接⼝:
package com.jsyd.ict.ictservicemanager.feign;
import com.jsyd.ict.sp.RespUtil;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
/
**
* 能⼒的调⽤
*
* @author wangyang
* @version 1.0
* @date 2021/3/11 21:01
*/
@FeignClient(name = "ict-service-capacity", configuration = MultipartSupportConfig.class)
public interface CapacityFeign {
@RequestMapping(value = "contentAudit/baiduImgAuditByFile", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) RespUtil baiduImgAuditByFile(@RequestPart(value = "file") MultipartFile file);
}
注意:⽂件的传输需要使⽤@RequestPart()注解
服务端:
@RequestMapping(value = "/baiduImgAuditByFile", method = RequestMethod.POST)
public RespUtil baiduImgAuditByFile(@RequestParam("file") MultipartFile file) {
// 调⽤service
return null;
}
结果发现,这样是获取不到结果的,研究了好⼀会,因为要着急联调,就没有继续跟踪了,有时间在深⼊研究下。
后来使⽤了下⾯的⽅法,成功传参
⽅法⼆:使⽤HttpServletRequest获取
上述操作都不需要,即:不需要导⼊依赖,也不需要创建⽂件,只需要在服务端通过HttpServletRequest去设置
使⽤HttpServletRequest设置:
伪代码如下:
@RequestMapping(value = "/baiduImgAuditByFile", method = RequestMethod.POST)
public RespUtil baiduImgAuditByFile(MultipartFile file, HttpServletRequest request) {
if (file == null) {
// 解决服务间调⽤⽆法接受MultipartFile参数的问题
String contentType = ContentType();springcloud和springboot
if (contentType != null && LowerCase().startsWith("multipart/")) {
MultipartHttpServletRequest multipartRequest =
if (multipartRequest != null) {
MultiValueMap<String, MultipartFile> multiFileMap = MultiFileMap();
for (Map.Entry<String, List<MultipartFile>> entry : Set()) {
file = Value().get(0);
}
}
}
}
// 处理业务逻辑
return null;
}
主要说明如下图:
即通过HttpServletRequest 的⽅式即使⽂件获取不到,也可以进⾏特殊处理,从⽽获取⽂件。
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论