java - 将 FormData 对象读入 Java HTTP 触发的 Azure Functions

标签 java azure azure-functions azure-blob-storage form-data

我最近创建了一个 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/

相关文章:

java - 如何使用 BufferedImageOp 在图像上应用黑白滤镜?

azure - 如何连接 Azure iot Central 中的网关

azure - 如何摆脱 Azure Powershell 警告 "WARNING: AzureVM A property of the output of this cmdlet will change in an upcoming..."

Azure 函数 - 主机 Json 文件丢失

azure - 如何使用azure逻辑应用程序操作在浏览器中下载文件

c# - Azure Function 到表存储

java - 终止期间线程延迟

java - 从 LDAP groupOfUniqueNames 中提取多值属性

JavaFX - 删除 TableView 中列之间的蓝线

python - 在 docker 镜像中安装 pyodbc