java - 如何从多部分文件中提取正文和文件名?
问题描述
我正在用 Java 编写 AWS lambda。此 lambda 充当APIGatewayProxyRequestEvent
.
API 网关端点将文件作为正文中的 multipart/form-data 发送。
public class LambdaHandler extends SpringBootRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
---
}
在尝试实现业务逻辑时,我首先将正文提取到一个字节数组中
byte[] file = Base64.decodeBase64(event.getBody().getBytes());
但是当我将这些字节写入字符串以从中提取数据时,我得到以下信息:
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--
如何从多部分文件的字节流中获取文件的唯一内容?
解决方案
我发现在使用 Java 时使用 AWS Lambda 处理 Multipart 文件的最佳解决方案是使用 Spring 云函数。请检查以下 GitHub 票证以确认功能可用性:
https://github.com/spring-cloud/spring-cloud-function/issues/597
但是,我还设法使用低级 API 使用核心 java 编写代码,因为该功能在我需要时不可用。我参考了这个博客。
推荐阅读
- ubuntu - 如何在 ubuntu 上保护自签名证书?
- php - 如何为单个数组中的数据存储相同的键
- php - 如何将未选中的复选框提交给控制器
- java - 使用 JOLT 使用嵌套数组对象转换对象数组
- bash - 使用 xmllint 查找多个值
- javascript - 无法将 node.js 应用程序从 es6 转换为 es5
- ssl - 收到错误“证书对 ServerCommonName 有效,而不是 ClientCommonName”
- c# - 如何通过累积概率选择随机项目?
- firebase - webhook 语音通话失败
- django - 过滤时如何在django中同时使用AND或OR条件