如何从Quarkus发送文件到Spring Boot应用?

huangapple go评论81阅读模式
英文:

How to send a file from Quarkus to a Spring Boot application?

问题

我正在编写一个Quarkus微服务,旨在与一个主要的Spring Boot应用程序进行通信。

为了调用Spring Boot应用程序,我根据这个Quarkus教程编写了一个基于REST的客户端,对于某些端点它可以正常工作。
问题出现在我尝试从Quarkus上传文件到Spring Boot时,无法使其正常工作。我按照这个其他教程来处理多部分请求。

这是我在Quarkus应用程序中的多部分对象:

public class MultipartBody {
    @FormParam("file")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    public InputStream file;

    @FormParam("fileName")
    @PartType(MediaType.TEXT_PLAIN)
    public String fileName;
}

这是Quarkus中REST客户端的端点:

@POST
@Path("/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
ProjectFile upload(@HeaderParam(AUTH_HEADER) String apiToken, @MultipartForm MultipartBody data);

这是我如何构建MultipartBody对象:

InputStream stream = IOUtils.toInputStream(contentString, Charset.defaultCharset());
MultipartBody data = MultipartBody.builder()
        .file(stream)
        .fileName(filename)
        .build();

Spring Boot应用程序中的端点:

@PostMapping("/file")
public ProjectFile receive(@RequestParam MultipartFile inputFile)

它抛出一个错误,说没有提供inputFile

Required request part 'inputFile' is not present - org.springframework.web.multipart.support.MissingServletRequestPartException - Required request part 'inputFile' is not present

如果我将@RequestParam更改为@RequestBody,那么inputFile参数始终为null。我漏掉了什么?

英文:

I'm writing a Quarkus microservice that is meant to communicate with a main Spring Boot application.

In order to make calls to the Spring Boot app, I wrote a REST client based on this Quarkus tutorial and it's working fine for some endpoints.
The problem happens when I try to upload a file from Quarkus to Spring boot, I cannot get it to work properly. I followed this other tutorial to work with multipart requests.

Here's my multipart object on my Quarkus application:

public class MultipartBody {
    @FormParam("file")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    public InputStream file;

    @FormParam("fileName")
    @PartType(MediaType.TEXT_PLAIN)
    public String fileName;
}

Here's the endpoint on REST client in Quarkus:

@POST
@Path("/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
ProjectFile upload(@HeaderParam(AUTH_HEADER) String apiToken, @MultipartForm MultipartBody data);

Here's how I build the MultipartBody object:

    InputStream stream = IOUtils.toInputStream(contentString, Charset.defaultCharset());
    MultipartBody data = MultipartBody.builder()
            .file(stream)
            .fileName(filename)
            .build();

The endpoint in Spring Boot application:

@PostMapping("/file")
public ProjectFile receive(@RequestParam MultipartFile inputFile)

It throws an error saying that inputFile is not provided:

Required request part 'inputFile' is not present - org.springframework.web.multipart.support.MissingServletRequestPartException - Required request part 'inputFile' is not present

If I change the @RequestParam for @RequestBody, then the inputFile parameter is always null. What am I missing?

答案1

得分: 1

使用MultipartFormDataOutput类时,多部分表单数据字段缺少文件名。在您的情况下,尝试使用@PartFilename注解。

> 必须与@MultipartForm一起使用。这为部分定义了文件名

try (InputStream fileInputStream = new FileInputStream(currentFile)) {

  MultipartFormDataOutput multipartFormDataOutput = new MultipartFormDataOutput();

  multipartFormDataOutput.addFormData("file", fileInputStream, MediaType.APPLICATION_OCTET_STREAM_TYPE, currentFile.getName());
  
  multipartFormDataOutput.addFormData("metaData", objectMapper.writeValueAsString(fileMeta), MediaType.APPLICATION_JSON_TYPE);

  // 调用客户端上传文件

}
英文:

Trying to use MultipartFormDataOutput class, multipart form data field is missing file name. In your case, trying to use @PartFilename annotation.

> Must be used in conjunction with @MultipartForm. This defines the filename for a part

try (InputStream fileInputStream = new FileInputStream(currentFile)) {

  MultipartFormDataOutput multipartFormDataOutput = new MultipartFormDataOutput();

  multipartFormDataOutput.addFormData("file", fileInputStream, MediaType.APPLICATION_OCTET_STREAM_TYPE, currentFile.getName());
  
  multipartFormDataOutput.addFormData("metaData",objectMapper.writeValueAsString(fileMeta),MediaType.APPLICATION_JSON_TYPE);

  // Call client to upload file

} 

huangapple
  • 本文由 发表于 2020年8月20日 22:43:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63507552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定