java.lang.ClassCastException:

任何人都可以帮助解决 EWS 中的以下问题,


我收到一个错误 java.lang.ClassCastException: microsoft.exchange.webservices.data.property.complex.ItemAttachment cannot be cast to microsoft.exchange.webservices.data.property.complex.FileAttachment


    private static void readAttachment(ExchangeService service, Folder folder, int n) {

    ItemView view = new ItemView(n);

    FindItemsResults<Item> findResults;

    try {

    findResults = service.findItems(folder.getId(), view);

    System.out.println("findResults-->"+findResults.getTotalCount());



    for (Item item : findResults) {

        EmailMessage email;


            email = EmailMessage.bind(service, item.getId(), new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));

            System.out.println("email.getAttachments().getCount()-->"+email.getAttachments().getCount());

            //email.load();

            for (Attachment it : email.getAttachments()) {

                System.out.println(it.getName());

                FileAttachment iAttachment = (FileAttachment) it;

                //iAttachment.load( "C:\\FileLocation\\" + iAttachment .getName());

            }


    }

    } catch (ServiceLocalException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    } catch (Exception e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

}


大话西游666
浏览 248回答 1
1回答

温温酱

这两个FileAttachment和ItemAttachment是直接的子Attachment类。它接缝一些附件是实例,ItemAttachment您正试图将它们转换为FileAttachment. 由于FileAttachmentclass 不是ItemAttachment您的子类,因此您无法对其进行强制转换。举个例子:public class Animal {&nbsp; }public class Dog extends Animal { }public class Cat extends Animal { }Animal a = new Dog();Cat c = (Cat)a; // this line produces ClassCastException as in your case在这个例子中你不能
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java