我正在尝试通过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();
}