How to extract body and file name from mulitpart file?

假如想象 提交于 2021-01-29 10:09:32

问题


I am writing an AWS lambada 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?


回答1:


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.



来源:https://stackoverflow.com/questions/64447770/how-to-extract-body-and-file-name-from-mulitpart-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!