gpt4 book ai didi

java - Azure HTTPTrigger 发布请求上传文件

转载 作者:行者123 更新时间:2023-12-01 18:12:57 26 4
gpt4 key购买 nike

我正在尝试通过 POST 请求上传文件。使用HttpRequestMessage<File>给我下面的错误。如果我使用 HttpRequestMessage<byte[]>该代码有效,但我缺少元数据,例如文件名和其他我需要上传的详细信息。

代码:

@FunctionName("HttpExample")
public String run(@HttpTrigger(name = "req", methods = { HttpMethod.GET,
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<File> request,
final ExecutionContext context) { ... }

错误:

Executed 'Functions.HttpExample' (Failed, Id=7349f061-9213-4607-a757-8231c41078d2)
[27/02/2020 10:18:15] System.Private.CoreLib: Exception while executing function: Functions.HttpExample. System.Private.CoreLib: Result: Failure
[27/02/2020 10:18:15] Exception: ClassCastException: Cannot convert com.microsoft.azure.functions.worker.binding.RpcHttpRequestDataSource@74386ca9to type com.microsoft.azure.functions.HttpRequestMessage<java.io.File>
[27/02/2020 10:18:15] Stack: java.lang.ClassCastException: Cannot convert com.microsoft.azure.functions.worker.binding.RpcHttpRequestDataSource@74386ca9to type com.microsoft.azure.functions.HttpRequestMessage<java.io.File>
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.binding.DataOperations.generalAssignment(DataOperations.java:191)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.binding.DataOperations.apply(DataOperations.java:120)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.binding.DataSource.computeByType(DataSource.java:56)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.binding.RpcHttpRequestDataSource.computeByType(RpcHttpRequestDataSource.java:20)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.binding.DataSource.computeByName(DataSource.java:42)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.binding.RpcHttpRequestDataSource.computeByName(RpcHttpRequestDataSource.java:20)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.binding.BindingDataStore.getDataByName(BindingDataStore.java:55)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.broker.ParameterResolver.resolve(ParameterResolver.java:59)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.broker.ParameterResolver.resolve(ParameterResolver.java:42)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.broker.JavaMethodExecutor.execute(JavaMethodExecutor.java:52)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:53)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.handler.MessageHandler.handle(MessageHandler.java:45)
[27/02/2020 10:18:15] at com.microsoft.azure.functions.worker.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:92)
[27/02/2020 10:18:15] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[27/02/2020 10:18:15] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[27/02/2020 10:18:15] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[27/02/2020 10:18:15] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[27/02/2020 10:18:15] at java.lang.Thread.run(Thread.java:748)
[27/02/2020 10:18:15] .

最佳答案

直接使用File是不可能的。没有实现将您的请求转换为 HttpRequestMessage<java.util.Optional<java.io.File>>

解决方案是使用 multipart/form-data 上传文件。已经有相关帖子了,可以引用:upload with multipart/form-data 。还有一个已知问题:Azure 函数会丢失一些不可读的字节,为了解决这个问题,您可能会在该答案中看到更新。

另一个更简单的解决方案是继续使用字节数组。但是,同时,您可以将文件名和其他元数据放入查询参数中。例如:?filename=xxx&other=xxxx

然后您可以从字节数组中获取文件内容,并从查询参数中获取所有元数据:

    @FunctionName("HttpExample")
public HttpResponseMessage run(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<byte[]>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");

byte[] body = request.getBody().get();

Map<String, String> queryParameters = request.getQueryParameters();
String fileName = queryParameters.get("fileName");

return request.createResponseBuilder(HttpStatus.OK).body(fileName + " -> " + new String(body)).build();
}

enter image description here

关于java - Azure HTTPTrigger 发布请求上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60430969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com