【问题标题】:Spring cloud feign not upload video fileSpring cloud feign 不上传视频文件
【发布时间】:2021-07-15 11:49:30
【问题描述】:

我有 2 个微服务。第一个服务(视频转换器)和第二个服务(Spring 与 Amazon S3 集成,在 Amazon S3 上存储视频和图像)。

第一个服务按计划生成文件,然后将 File 转换为 MultipartFile 后将其上传到第二个服务,然后再上传到 Amazon S3。

但我认为 Feigh 上传视频的问题。 调试的时候看到第二个服务@RequestPart里面的文件是null。但是转换成功了。

我尝试像File upload spring cloud feign client 那样添加编码,但这没有帮助。

代码示例第一个微服务: 上传方式:

    @Override
    public Response<String> uploadFile(final Object object, final MultipartFile multipartFile) {
        final Map<String, Object> s3Url = videoServiceFeign.uploadFile(multipartFile,
                object.getId().toString(), object.getIdIds().toString(), object.getName());
        object.setS3Url(s3Url.get("object").toString());
        return generateSuccessResponse();
    }

    @Override
    public Response<String> uploadFile(final Stream stream, final File file) throws IOException {
        final InputStream inputStream = Files.newInputStream(file.toPath());
        final MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                ContentType.MULTIPART_FORM_DATA.toString(), IOUtils.toByteArray(inputStream));
        return this.uploadFile(stream, multipartFile);
    }

假装客户端:

@FeignClient(name = "video-service", url = "${video-service.ribbon.listOfServers}")
public interface VideoServiceFeign {

    @PostMapping(path = "/api/v1/video/upload", consumes = {"multipart/form-data"})
    Map<String, Object> uploadFile(@RequestPart("file") MultipartFile multipartFile,
                                   @RequestParam("id") String id,
                                   @RequestParam("ids") String ids,
                                   @RequestParam("name") String name);

}

在第二个服务中上传端点:

    @ApiOperation("Upload new video file")
    @ApiResponses({
            @ApiResponse(code = 200, message = "File uploaded successfully"),
            @ApiResponse(code = 403, message = "Access Denied"),
            @ApiResponse(code = 500, message = "Internal server error"),
            @ApiResponse(code = 503, message = "Gateway timeout")
    })
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/upload", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
    public @ResponseBody
    Map<String, Object> uploadFile(@RequestPart("file") final MultipartFile file,
                                   @RequestParam("id") final String id,
                                   @RequestParam("ids") final String ids,
                                   @RequestParam("name") final String name
                                ) throws IOException {
        return uploadService.uploadFile(id, ids, name, file);
    }

我错过了什么?

【问题讨论】:

    标签: spring spring-boot spring-cloud-feign feign


    【解决方案1】:

    经过几天的研究,我发现了我想念的东西,问题出在那个方法上:public Response&lt;String&gt; uploadFile(final Stream stream, final File file) throws IOException

    在行中:

    final MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), ContentType.MULTIPART_FORM_DATA.toString(), IOUtils.toByteArray(inputStream));
    

    我有两个问题首先是内容类型,我使用video/mp4 而不是ContentType.MULTIPART_FORM_DATA。 第二个是MockMultipartFile 的构造函数中的第一个参数我使用file.getName(),但这是不正确的,因为文档说这是多部分请求中的字段名称。那应该是“文件”。

    【讨论】:

      猜你喜欢
      • 2018-04-01
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 2015-08-24
      • 2017-07-23
      • 2015-10-21
      • 2018-08-31
      相关资源
      最近更新 更多