2017-03-17 46 views
0

我需要識別附件是發件人的簽名圖像並忽略它,因爲我想跳過那種附件,但我無法識別是否該特定的附件是簽名圖像。Java EWS-如何識別附件是否爲發件人簽名圖像

或者用戶可以在添加簽名圖像的同時添加自定義屬性,以便我可以在程序中查找該屬性?

if (emailMessage.getHasAttachments() || emailMessage.getAttachments().getItems().size() > 0) { 

//get all the attachments 
AttachmentCollection attachmentsCol = emailMessage.getAttachments(); 

log.info("File Count: " + attachmentsCol.getCount()); 

    Attachment attachment = attachmentsCol.getPropertyAtIndex(i); 
    //log.debug("Starting to process attachment "+ attachment.getName()); 

    //do we need to skip this attachment 

     FileAttachment fileAttachment = (FileAttachment) attachment; 
     // if we don't call this, the Content property may be null. 
     fileAttachment.load(); 
     booelan isSignatureImage = fileAttachment.isContactPhoto(); // this is false 
} 

}

回答

0

我想出了一個辦法做到這一點。運行代碼一次,確定要忽略的附件的散列,然後製作該散列的本地字符串。然後,處理每個附件,將其與本地散列字符串進行比較,如果它們匹配,則可以忽略它並轉到下一個(如果您正在像我一樣處理循環中的附件)。

  // you must first load your attachment to be able to call .getContent() on it 
      fileAttachment.load(); 

      // load content 
      byte[] b = fileAttachment.getContent(); 

      // get the hash of your attachment 
      byte[] hash = MessageDigest.getInstance("MD5").digest(b); 

      // after you run this on the attachment you do not want, take the string from 'actual' below and put it here 
      String expected = "YOUR HASHED STRING"; 

      String actual = DatatypeConverter.printHexBinary(hash); 
      System.out.println(actual); 

      // some conditional to check if your current hash matches expected 
      if(actual.equals(expected)){ 
       continue; 
      }