Feign的多部分文件上传偶发失败,抛出RetryableException:输出流不完整

后端开发 2026-07-10

我在使用OpenFeign在两个Spring Boot微服务之间上传文件时遇到问题。

  1. task-service生成一个Excel文件
  2. 它使用Feign (multipart/form-data) 将文件发送到system-service

System-Service将文件上传到AWS S3

Feign Client

@FeignClient(
    name = "system-service",
    url = "${feign.client.main.url.system}",
    configuration = FeignMultipartConfig.class
)
public interface SystemServiceClient {

    @PostMapping(
        value = "/system-service/uploadFile",
        consumes = MediaType.MULTIPART_FORM_DATA_VALUE
    )
    Map<String, Object> uploadFile(
        @RequestPart("file") Resource file,
        @RequestHeader MultiValueMap<String, String> headers
    );
}

正在发送的文件

ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(tempFile)) {
    @Override
    public String getFilename() {
        return fileName;
    }
};

system-service的控制器

@PostMapping("/uploadFile")
public Map<String, Object> uploadFile(
        @RequestPart("file") MultipartFile file,
        @RequestHeader MultiValueMap<String, String> headers) {
    return awsService.uploadFile(file, headers);
}

AWS上传

s3Client.putObject(
        request,
        RequestBody.fromInputStream(
            file.getInputStream(),
            file.getSize()
        )
);

问题

上传有时能成功,但偶尔会失败,错误信息如下:

java.util.concurrent.ExecutionException: feign.RetryableException: Incomplete output stream executing POST http://localhost:9008/system-service/uploadFile

解决方案

异常与GitHub问题

你的问题与在 : https://github.com/spring-cloud/spring-cloud-openfeign/issues/390 上报告的异常相同

feign.RetryableException: Incomplete output stream executing POST feign.RetryableException: Incomplete output stream executing POST http://topic-provider/subjective/recitation/bringback/topic at feign.FeignException.errorExecuting(FeignException.java:132) at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:113)

票数最高的评论如下:https://github.com/spring-cloud/spring-cloud-openfeign/issues/390#issuecomment-685224992

tangchen-blip于 2020年 9月 2日

移除header参数中的content-length可以解决它

因此,我认为你应该进行修改,移除(或修改):

@RequestHeader MultiValueMap headers

该讨论中的另一条评论提出:

headers.remove("content-length");

//Then call the final upload

调试你的请求头

我还建议你为请求开启调试。 请参考StackOverflow链接:Feign日志在更改级别时不起作用

但请使用FULL级别。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章