简体   繁体   中英

How to extract body and file name from mulitpart file?

I am writing an AWS lambda in Java. This lambda acts as a handler for APIGatewayProxyRequestEvent .

API gateway endpoint is sending a file as a multipart/form-data in the body.

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. Please check the following GitHub ticket confirming the feature availability :

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. I refered this blog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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