有人可以让我知道如何从Java中的HTTP Trigger Azure函数发送pdf响应。
一旦触发该功能,我需要从Azure存储中读取pdf并将pdf作为响应返回给浏览器。
答案 0 :(得分:1)
这对我来说很好:
package com.function;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.util.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;
public class HttpTriggerJava {
@FunctionName("HttpTriggerJava")
public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.GET,
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
try {
CloudStorageAccount storageAccount = CloudStorageAccount.parse(
"{your storage connections tring}");
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference("{container name}");
CloudBlockBlob blob = container.getBlockBlobReference("{filename}");
byte[] content = new byte[blob.getStreamWriteSizeInBytes()];
blob.downloadToByteArray(content, 0);
return request.createResponseBuilder(HttpStatus.OK)
.body(content)
.build();
} catch (InvalidKeyException | URISyntaxException e) {
e.printStackTrace();
return request.createResponseBuilder(HttpStatus.NOT_FOUND)
.body(e.getMessage())
.build();
} catch (StorageException e) {
e.printStackTrace();
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.body(e.getMessage())
.build();
}
}
}