当前位置: 代码迷 >> 综合 >> Feign 传输 MultipartFile 的一些问题
  详细解决方案

Feign 传输 MultipartFile 的一些问题

热度:80   发布时间:2023-12-13 05:39:48.0

Feign 传输 MultipartFile 的一些问题

File 转 MultipartFile

pom.xml

<!-- https://mvnrepository.com/artifact/org.springframework/spring-mock -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
public static MultipartFile getMultipartFile(String fileName, File file) throws IOException {
    return new MockMultipartFile(fileName, file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(file));
}

报错 Current request is not a multipart request、Content type ‘’ not supported

  • @PostMapping设置 consumes = MediaType.MULTIPART_FORM_DATA_VALUE
  • 使用@RequestPart(),不能使用@RequestParam()
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResultBody upload(@RequestPart(value = "file") MultipartFile file);

报错 Required request part ‘file’ is not present

configuration

@Configuration
public class UploadFeignConfig {
    @Beanpublic Encoder multipartFormEncoder() {
    return new SpringFormEncoder(new SpringEncoder(new ObjectFactory<HttpMessageConverters>() {
    @Overridepublic HttpMessageConverters getObject() throws BeansException {
    return new HttpMessageConverters(new RestTemplate().getMessageConverters());}}));}
}

FeignClient

@FeignClient(value = FileConstants.FILE_SERVER, configuration= UploadFeignConfig.class)
public interface FileServiceClient extends IFileServiceClient {
    @Override@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)ResultBody upload(@RequestPart(value = "file") MultipartFile file);
}
  相关解决方案