我正在按照这个 Teams 文件上传 Bot 示例构建一个 Teams Bot。
我正在尝试获取 PDF 文件,并让它通过 FileConsentCard 和 FileInfoCard 进行响应:
单击提供的内容 URL 链接时,出现以下错误,无法查看文件或下载。
我使用的代码与文件上传机器人示例相同,只是我将文件类型更改为我的网络服务器上的 PDF 文档。
关于为什么链接无效的任何想法?可能有一些权限?
private CompletableFuture<Void> sendFileCard(
TurnContext turnContext, String filename, long filesize
) {
Map<String, String> consentContext = new HashMap<>();
consentContext.put("filename", filename);
FileConsentCard fileCard = new FileConsentCard();
fileCard.setDescription("This is the file I want to send you");
fileCard.setSizeInBytes(filesize);
fileCard.setAcceptContext(consentContext);
fileCard.setDeclineContext(consentContext);
Attachment asAttachment = new Attachment();
asAttachment.setContent(fileCard);
asAttachment.setContentType(FileConsentCard.CONTENT_TYPE);
asAttachment.setName(filename);
Activity reply = turnContext.getActivity().createReply();
reply.setAttachments(Collections.singletonList(asAttachment));
return turnContext.sendActivityBlind(reply);
}
private CompletableFuture<ResultPair<String>> upload(
FileConsentCardResponse fileConsentCardResponse
) {
AtomicReference<ResultPair<String>> result = new AtomicReference<>();
return CompletableFuture.runAsync(() -> {
Map<String, String> context = (Map<String, String>) fileConsentCardResponse
.getContext();
File filePath = new File("files", context.get("filename"));
HttpURLConnection connection = null;
try {
URL url = new URL(fileConsentCardResponse.getUploadInfo().getUploadUrl());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Length", Long.toString(filePath.length()));
connection.setRequestProperty(
"Content-Range",
String.format("bytes 0-%d/%d", filePath.length() - 1, filePath.length())
);
try (
FileInputStream fileStream = new FileInputStream(filePath);
OutputStream uploadStream = connection.getOutputStream()
) {
byte[] buffer = new byte[4096];
int bytes_read;
while ((bytes_read = fileStream.read(buffer)) != -1) {
uploadStream.write(buffer, 0, bytes_read);
}
}
result.set(new ResultPair<String>(true, null));
} catch (Throwable t) {
result.set(new ResultPair<String>(false, t.getLocalizedMessage()));
} finally {
if (connection != null) {
connection.disconnect();
}
}
})
.thenApply(aVoid -> result.get());
}
private CompletableFuture<Void> fileUploadCompleted(
TurnContext turnContext, FileConsentCardResponse fileConsentCardResponse
) {
FileInfoCard downloadCard = new FileInfoCard();
downloadCard.setUniqueId(fileConsentCardResponse.getUploadInfo().getUniqueId());
downloadCard.setFileType(fileConsentCardResponse.getUploadInfo().getFileType());
Attachment asAttachment = new Attachment();
asAttachment.setContent(downloadCard);
asAttachment.setContentType(FileInfoCard.CONTENT_TYPE);
asAttachment.setName(fileConsentCardResponse.getUploadInfo().getName());
asAttachment.setContentUrl(fileConsentCardResponse.getUploadInfo().getContentUrl());
Activity reply = MessageFactory.text(
String.format(
"<b>File uploaded.</b> Your file <b>%s</b> is ready to download",
fileConsentCardResponse.getUploadInfo().getName()
)
);
reply.setTextFormat(TextFormatTypes.XML);
reply.setAttachment(asAttachment);
return turnContext.sendActivityBlind(reply);
}