如何从mulitpart文件中提取正文和文件名? [英] How to extract body and file name from mulitpart file?

查看:85
本文介绍了如何从mulitpart文件中提取正文和文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写AWS lambada.此lambda充当APIGatewayProxyRequestEvent的处理程序.API网关端点正在将文件作为主体中的multipart/form-data发送.

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?

推荐答案

我发现在使用Java时使用AWS Lambda处理Multipart文件的最佳解决方案是使用Spring云功能.请检查以下GitHub票证,以确认功能可用:

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

但是,我还设法使用低级API使用核心Java编写了代码,因为该功能在我需要时不可用.我引用了

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.

这篇关于如何从mulitpart文件中提取正文和文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆