我最近创建了一个 HTTP 触发的 Java Azure 函数来处理我的一些后端任务。这个 Azure-Functions 的主要目的是接受/读取封装在 javascript FormData 对象中的图像文件 并将文件上传到我的 azure blob 存储。
我一直在网上搜索,但无法找到如何从 java 中的 FormData 对象读取文件的工作示例?我见过一些使用 C# 中的 azure 函数完成此操作的示例, 节点.js。
这是链接 --> Azure functions - How to read form data
所以必须有办法在 Java 中做到这一点,有人有或者有人可以提供一个工作示例吗?
public class Function {
@FunctionName("HttpTrigger-Java")
public HttpResponseMessage run(@HttpTrigger(name = "req", methods =
{HttpMethod.POST}) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
String postRequest = request.getBody();
//HELP: I need to get the FormData object from the postRequest some how...
FormData formDataObj = postRequest.getFormData();
//HELP: Then I need to get my image file from the FormData Object...
File sourceFile = formDataObj.getImageFile();
try {
// Parse the connection string
storageAccount = CloudStorageAccount.parse(storageConnectionString);
// get blob client
blobClient = storageAccount.createCloudBlobClient();
// get container name
container = blobClient.getContainerReference(containerName);
//Getting a blob reference
blob = container.getBlockBlobReference(sourceFile.getName());
//Creating blob and uploading file to it
blob.uploadFromFile(sourceFile.getAbsolutePath());
} catch (StorageException ex) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("There was an error with Storage Account.").build();
}
return request.createResponseBuilder(HttpStatus.OK).body("Images Uploaded Successfully!, ").build();
}
}
最佳答案
我相信你可以使用 Apache Commons FileUpload 解析正文要从字符串中执行此操作,此 answer展示了一种方法来做到这一点。
关于java - 将 FormData 对象读入 Java HTTP 触发的 Azure Functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60766872/