英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论