首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在包含contentBytes的Java SDK中获取FileAttachment对象?

如何在包含contentBytes的Java SDK中获取FileAttachment对象?
EN

Stack Overflow用户
提问于 2019-08-19 14:18:34
回答 2查看 392关注 0票数 1

我正在尝试使用Microsoft Graph SDK让contentBytes下载办公邮件附件。

我尝试了下面的方法,但它给出了ClassCastException。我不知道如何通过Microsoft Graph SDK(Java)直接获取FileAttachment对象的列表

代码语言:javascript
复制
IAttachmentCollectionPage aAttachementPage = clientAuthGraphServiceClient.users("#userIDGiven#").mailFolders("Inbox").messages(message.id).attachments().buildRequest().get();

for (Attachment aAttachment:aAttachementPage.getCurrentPage()) {
   String serviceDirectory="D:\\DOWNLOADS\\";
   File fileDown= new File(serviceDirectory+aAttachment.name);
   Files.write(fileDown.toPath(), ((FileAttachment)aAttachment).contentBytes);
}

我正在使用以下jar:

代码语言:javascript
复制
    <dependency>
        <groupId>com.microsoft.graph</groupId>
        <artifactId>microsoft-graph</artifactId>
        <version>1.4.0</version>
    </dependency>
EN

回答 2

Stack Overflow用户

发布于 2019-08-30 11:08:34

有几种派生的依恋类型。

  • 文件Attachment
  • Item Attachment
  • Reference Attachment

https://docs.microsoft.com/en-us/graph/api/resources/attachment?view=graph-rest-1.0

如果任何附件不是FileAttachment,那么您将得到这种类型的强制转换异常。在尝试将附件转换为FileAttachment之前,您需要测试附件的类型。

票数 0
EN

Stack Overflow用户

发布于 2022-03-01 09:04:14

我知道这是一个古老的问题,但我偶然发现了这个问题,并认为我可以分享我们的解决方案。注意NPE或IndexOutOfBoundsExceptions。

代码语言:javascript
复制
    MessageCollectionPage messageCollectionPage = graphClient.me().messages().buildRequest().get();

    List<String> messageIdsWithAttachments =
        messageCollectionPage.getCurrentPage().stream()
            .filter(message -> message.hasAttachments)
            .map(message -> message.id)
            .toList();

    AttachmentCollectionPage attachmentCollectionPage =
        graphClient
            .me()
            .messages(messageIdsWithAttachments.get(0))
            .attachments()
            .buildRequest()
            .get();

    byte[] bytes = ((FileAttachment) attachmentCollectionPage.getCurrentPage().get(0)).contentBytes;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57551352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档