任何人都可以幫助解決 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(); }}
1 回答

溫溫醬
TA貢獻1752條經(jīng)驗 獲得超4個贊
這兩個FileAttachment和ItemAttachment是直接的子Attachment類。它接縫一些附件是實例,ItemAttachment您正試圖將它們轉(zhuǎn)換為FileAttachment. 由于FileAttachmentclass 不是ItemAttachment您的子類,因此您無法對其進行強制轉(zhuǎn)換。
舉個例子:
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
在這個例子中你不能
添加回答
舉報
0/150
提交
取消