第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Microsoft Graph SDK Java:如何在包含 contentBytes

Microsoft Graph SDK Java:如何在包含 contentBytes

幕布斯7119047 2023-06-14 15:32:45
我正在嘗試使用 Microsoft Graph SDK 獲取 contentBytes 以下載辦公郵件附件。我嘗試了以下但它給出了 ClassCastException。我不知道如何通過 Microsoft Graph SDK(Java) 直接獲取 FileAttachment 對(duì)象列表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);}我正在使用以下罐子:    <dependency>        <groupId>com.microsoft.graph</groupId>        <artifactId>microsoft-graph</artifactId>        <version>1.4.0</version>    </dependency>
查看完整描述

4 回答

?
PIPIONE

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊

對(duì)于 GraphApi 版本 2.5.0

AttachmentCollectionPage attachmentCollectionPage =
    graphClient
        .me()
        .messages(messageId)
        .attachments()
        .buildRequest()
        .get();    JsonElement att = attachmentCollectionPage
        .getCurrentPage()
        .get(0)
        .getRawObject()
        .get("contentBytes"); 
               byte[] decodedBytes = Base64.getDecoder().decode(att.getAsString());


查看完整回答
反對(duì) 回復(fù) 2023-06-14
?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊

每隔一段時(shí)間,我都會(huì)對(duì)著 Microsoft 文檔大罵 Java 實(shí)現(xiàn)(大多數(shù)時(shí)候是不完整的),而且我花在升級(jí)客戶端上的時(shí)間比我想花的時(shí)間還長(zhǎng)。所以我在這里復(fù)制我自己的答案??赡苡腥擞X得這很有用。


從 microsoft-graph 版本 5.34.0 開始,不再需要一些東西,比如解碼,您將能夠簡(jiǎn)單地轉(zhuǎn)換您的對(duì)象。


獲取 File/ItemAttachments 的復(fù)雜方法:


List<Attachment> attachments = new ArrayList<>();


        try {

            AttachmentCollectionPage mgAttachmentList =

                    graphClient.users(emailAddress)

                            .messages(mail.id)

                            .attachments()

                            .buildRequest()

                            .get();


            do {

                attachments.addAll(mgAttachmentList.getCurrentPage());


                // Check for next page

                if (mgAttachmentList.getNextPage() != null) {

                    mgAttachmentList = mgAttachmentList.getNextPage().buildRequest().get();

                } else {

                    mgAttachmentList = null;

                }

            } while (mgAttachmentList != null);


            attachments.stream()

                    .filter(Objects::nonNull)

                    .forEach(attachment -> {

                        try {

                            // attachment equals a FileAttachment

                            if ("#microsoft.graph.fileAttachment".equalsIgnoreCase(attachment.oDataType)) {

                                byte[] attachmentContentBytes = ((FileAttachment) attachment).contentBytes;

                                if (attachmentContentBytes != null) {

                                    // your own logic here

                                }

                            } else if ("#microsoft.graph.itemAttachment".equalsIgnoreCase(attachment.oDataType)) {

                                /*

                                 *  We need to convert each Attachment.

                                 *  If it's an ItemAttachment, we need to do another call to retrieve the item-object.

                                 *

                                 *  The standard call with expand option gives us an Attachment (again)

                                 */

                                if (StringUtils.isNotBlank(attachment.contentType)) {

                                    Attachment attachment = graphClient.users(emailAddress)

                                            .messages(mail.id)

                                            .attachments(attachment.id)

                                            .buildRequest()

                                            .expand("microsoft.graph.itemattachment/item")

                                            .get();

                                    if (attachment != null) {

                                        // cast the object to whatever you need, in this example I retrieve the webLink

                                        String linkToMessage = ((Message) ((ItemAttachment) attachment).item).webLink;

                                        // your own logic here

                                    }

                                } else {

                                    log.info("Unknown attachment contentType for an ItemAttachment - Could not read attachment.");

                                }

                            } else {

                                log.error("Unable to read an attachment for mail.");

                            }

                        } catch (Exception e) {

                            log.error("Could not read attachment: '" + attachment.name + "' for message: '" + message.subject + ". " + e.getMessage(), e);

                        }

                    });

        } catch (Exception e) {

            log.error("Something went wrong while receiving attachments for message: '" + message.subject + "'. " + e.getMessage(), e);

        }


查看完整回答
反對(duì) 回復(fù) 2023-06-14
?
交互式愛情

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊

我知道這是一個(gè)老問題,但我偶然發(fā)現(xiàn)了這個(gè)問題并認(rèn)為我可以分享我們的解決方案。注意 NPE 或 IndexOutOfBoundsExceptions。


    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;


查看完整回答
反對(duì) 回復(fù) 2023-06-14
?
烙印99

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊

有幾種派生類型的附件。

  • 文件附件

  • 項(xiàng)目附件

  • 參考附件

如果任何附件不是 FileAttachment,那么您將得到這種強(qiáng)制轉(zhuǎn)換異常。在嘗試將其轉(zhuǎn)換為 FileAttachment 之前,您需要測(cè)試附件的類型。


查看完整回答
反對(duì) 回復(fù) 2023-06-14
  • 4 回答
  • 0 關(guān)注
  • 285 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)