【问题标题】:java.lang.ClassCastException: ItemAttachment cannot be cast to FileAttachment [duplicate]java.lang.ClassCastException:ItemAttachment 无法转换为 FileAttachment [重复]
【发布时间】:2019-02-04 18:08:47
【问题描述】:

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

我收到错误 java.lang.ClassCastException:microsoft.exchange.webservices.data.property.complex.ItemAttachment 无法转换为 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();
    }
}

【问题讨论】:

  • 您能否更具体地说明错误消息的哪一部分让您感到困惑?在对该异常进行一些研究后,您能否告诉我们您为解决该问题所做的尝试?
  • 在这一行 FileAttachment iAttachment = (FileAttachment) it;

标签: java exchangewebservices


【解决方案1】:

FileAttachmentItemAttachment 都是 Attachment 类的直接子类。它接缝了一些附件是ItemAttachment 的实例,您正试图将它们转换为FileAttachment。由于FileAttachment 类不是ItemAttachment 的子类,因此您不能对其进行强制转换。

举个例子:

public class Animal {  }
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

在这个例子中你不能

【讨论】:

  • 谢谢!我删除了邮箱中的一个 ItemAttachment。之后它就完美运行了。
猜你喜欢
  • 1970-01-01
  • 2018-11-07
  • 2017-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多