简体   繁体   English

如何从多部分文件中提取正文和文件名?

[英]How to extract body and file name from mulitpart file?

I am writing an AWS lambda in Java.我正在用 Java 编写 AWS lambda。 This lambda acts as a handler for APIGatewayProxyRequestEvent .此 lambda 充当APIGatewayProxyRequestEvent的处理程序。

API gateway endpoint is sending a file as a multipart/form-data in the body. API 网关端点正在将文件作为多部分/表单数据发送到正文中。

public class LambdaHandler extends SpringBootRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
   ---

}

while trying to implement the business logic, I am first extracting the body into an array of bytes在尝试实现业务逻辑时,我首先将主体提取到一个字节数组中

byte[] file = Base64.decodeBase64(event.getBody().getBytes());

But when I write these bytes to a string for extracting the data from it, I get the following :但是当我将这些字节写入一个字符串以从中提取数据时,我得到以下信息:

log.info("file content : {}", new String(file));

output:


----------------------------728667852190241466147817
Content-Disposition: form-data; name=""; filename="testuplode.json"
Content-Type: application/json

this is a test file
----------------------------728667852190241466147817--

How to fetch the only content of file from a byte stream of multipart file?如何从多部分文件的字节流中获取文件的唯一内容?

The best solution I found for handling Multipart file with AWS Lambda while working with Java is to use the Spring cloud functions.我发现在使用 Java 时使用 AWS Lambda 处理多部分文件的最佳解决方案是使用 Spring 云函数。 Please check the following GitHub ticket confirming the feature availability :请检查以下 GitHub 票证以确认功能可用性:

https://github.com/spring-cloud/spring-cloud-function/issues/597 https://github.com/spring-cloud/spring-cloud-function/issues/597

However, I also managed to write the code with core java using low-level API's as the feature was not available back when I needed it.但是,我还设法使用低级 API 使用核心 java 编写代码,因为该功能在我需要时无法使用。 I refered this blog.我参考了这个博客。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM